An ai intelligent agent memory management method and device based on a postgresql database
By using the current valid flag and version chain information in the PostgreSQL database, combined with logical decoding function and consumer service, the problem of memory consistency management of AI intelligent agent is solved, realizing versioned storage and consistency management of memory state, and supporting state maintenance and fault reproduction during tasks.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HIGHGO SOFTWARE
- Filing Date
- 2026-06-01
- Publication Date
- 2026-07-24
AI Technical Summary
Existing technologies are insufficient to provide AI intelligent agents with long-term, manageable memory capabilities, resulting in the loss of historical memory states and difficulty in ensuring consistency, which affects the user experience.
Using a PostgreSQL database, versioned storage and state consistency management of memories are achieved by setting current validity flags and version chain information in the memory table, combined with logical decoding functions and independent consumer services. This includes the export of transaction snapshots and the use of audit log tables to ensure the consistency and traceability of memory states.
It achieves versioned storage and consistency management of the AI intelligent agent's memory state, ensuring that the memory state remains consistent with the task start time throughout the task lifecycle, supports the reproduction and rollback of fault scenarios, and solves the problem of historical memory state loss.
Smart Images

Figure CN122285707B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database technology, and in particular to an AI intelligent agent memory management method and device based on PostgreSQL database. Background Technology
[0002] AI intelligent agent memory is a structured data entity, which can be the specific information ontology of memory, usually stored in text form, such as the user's interaction history with AI, task execution results, learned knowledge or rules, etc.
[0003] With the development of AI intelligent agent technology, endowing AI intelligent agents with long-term and manageable memory capabilities has become a key challenge. AI intelligent agent memory systems not only need to store and retrieve historical interactions, knowledge, and states, but also need to meet engineering requirements such as debuggability, auditability, and high consistency. Currently, the management of AI intelligent agent memory has significant limitations, leading to a poor user experience. Summary of the Invention
[0004] This specification provides one or more embodiments of an AI intelligent agent memory management method and device based on a PostgreSQL database, which is used to solve the technical problems mentioned in the background.
[0005] One or more embodiments of this specification employ the following technical solutions: This specification provides one or more embodiments of an AI intelligent agent memory management method based on a PostgreSQL database, the method comprising: A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions. When a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent's memory; A logical replication slot is created based on the logical decoding function of the relational database, and the data changes of the memory table are continuously captured using the logical replication slot to obtain change events; The change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier, and complete row data after the change. The structured information is then persistently stored in the audit log table. When the AI intelligent agent starts executing a specified task, a specified transaction snapshot identifier is exported from the relational database. During the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction to ensure that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task. Based on the version chain information in the audit log table and / or the memory table, perform backtracking and / or rollback operations on the memory state.
[0006] It should be noted that this application sets a current validity flag and version chain information in the memory table, enabling each memory change to be persistently stored as an independent new record, effectively solving the technical problem of historical memory state loss in traditional solutions. Simultaneously, this application utilizes the logical decoding function of a relational database to create a logical replication slot, and through an independent consumer service, parses captured change events into structured information and stores it in the audit log table, providing accurate time information for subsequent state backtracking. Furthermore, when the AI intelligent agent begins executing a specified task (such as a complex multi-step task), a specified transaction snapshot identifier can be exported from the database and imported into the new transaction before each memory query during task execution, ensuring that the agent's memory state remains consistent with the task's start time throughout the task's lifecycle. Finally, by combining the structured change records in the audit log table with the version chain information in the memory table, the complete memory at any historical moment can be reconstructed, enabling the reproduction of fault scenarios, or, by batch updating the current validity flag, safely rolling back the entire memory system to a specified historical version.
[0007] Furthermore, the version chain information includes a version number field and a parent version identifier field; wherein, the version number field is used to identify the version order of the memory record, and the parent version identifier field is used to point to the record identifier of the previous version from which the memory record originates.
[0008] It should be noted that the version number field in this application can represent the chronological order between different versions, and the parent version identifier field can represent the relationship between the current version and the previous version. This allows each newly created memory record to be traced back to the previous version from which it originated. When historical state reconstruction or fault reproduction is required, it is no longer necessary to rely on external logs; instead, the search can be performed directly along the parent version identifier field, while the correctness of the order can be confirmed based on the version number field.
[0009] Furthermore, when a memory change request is received, creating a new record in the memory table containing the currently valid flag and the version chain information through a database transaction includes: When the memory change request is a new memory request, a new record is inserted into the memory table, and an initial version number is set for the new record, and the parent version identifier field of the new record is empty.
[0010] It's important to note that when a memory change request is determined to be a new memory request, a new record is directly inserted into the memory table, with an initial version number set and the parent version identifier field left null. This indicates that the new memory is the starting node of the entire version chain, with no preceding versions to trace, thus avoiding redundant steps such as freezing old versions and querying parent version numbers for the new operation. Furthermore, this operation can be completed with a single insert statement within a single database transaction, ensuring that the process of writing new memories remains within the same data model as the existing versioned update mechanism.
[0011] Furthermore, when the memory change request is a memory update request, the method further includes: In a database transaction, the current valid flag of the original record to be updated in the memory table is updated to an invalid state; In the database transaction, a new record is inserted into the memory table. The content of the new record is the updated data. The version number of the new record is generated by incrementing the version number of the original record. The parent version identifier field of the new record points to the identifier of the original record, and the current validity flag of the new record is set to a valid state.
[0012] It should be noted that when an update memory request is received, this application updates the current validity flag of the original record to an invalid state, and then inserts a new record. The content of this new record is the updated data, and its version number is generated by incrementing the version number of the original record. Furthermore, its parent version identifier field points to the identifier of the original record. This ensures that the entire update process either succeeds entirely or is rolled back entirely. If it is necessary to locate the current valid memory later, it can be quickly retrieved by querying the record where the current validity flag is true.
[0013] Furthermore, the step of exporting a specified transaction snapshot identifier from the relational database includes: The snapshot identifier of the specified transaction is obtained by calling the snapshot export function of the relational database.
[0014] It should be noted that this application can directly call the built-in snapshot export function of the relational database to obtain the snapshot identifier of the specified transaction, which can avoid state deviation caused by external intervention, and thus provide a stable and reliable memory context for AI intelligent agents during the execution of complex multi-step tasks.
[0015] Furthermore, the backtracking of the memory state includes: Based on a specified historical time point, the audit log table is queried, and the complete memory table state at the specified historical time point is reconstructed by replaying the change events that occurred before the specified historical time point.
[0016] It should be noted that this application can query the audit log table based on a specified historical time point, filter out all change events that occurred before that time point, and thus reconstruct the complete memory table state at that moment. Because the audit log table persistently stores structured change events, each event contains operation type, change time, and complete row data, it is possible to accurately restore a memory snapshot of any historical moment in a deterministic manner, independent of the current physical storage state of the database.
[0017] Furthermore, the method also includes: When a specific transaction snapshot identifier derived by the AI intelligent agent near the specified historical time point is obtained, the specific transaction snapshot identifier is imported into an isolated database transaction to reproduce the consistent memory state view on which the AI intelligent agent made decisions at the historical time point.
[0018] It should be noted that, in addition to reconstructing the memory table state at a specified historical point in time, this application also needs to obtain specific transaction snapshot identifiers exported by the AI intelligent agent near that point in time and import these identifiers into an isolated database transaction. Therefore, the obtained memory view not only contains the data content at the historical point in time but also replicates the data consistency boundaries actually seen by the agent at that time. This is because the transaction snapshot identifier encodes the precise state of all active transactions in the database at the time of export. After importing, the visibility rules of all queries are set to be exactly the same as at the time of the agent's decision, thereby enabling the complete replication of the consistent memory context on which the agent made its decisions in an isolated environment.
[0019] Furthermore, a rollback is performed on the memorized state, including: Determine the target historical version to which you need to roll back; In a database transaction, all records in the memory table whose current valid flag is valid are updated to invalid, and the current valid flag of the memory records belonging to the target historical version is updated to valid.
[0020] It should be noted that this application first sets all records with currently valid flags to invalid, and then sets the records corresponding to the target historical version to valid. Since the entire process is atomic, it can effectively prevent inconsistencies caused by mixing old and new version states during rollback. Furthermore, this operation only modifies the flag field, thus enabling the switching of the global memory state with minimal write overhead.
[0021] Furthermore, updating the current valid flag of the memory record belonging to the target historical version to valid includes: Based on the version chain formed by the version number field and / or the parent version identifier field, locate all memory records whose version numbers correspond to the target historical version, and update the current validity flag of these records to valid.
[0022] It should be noted that when it is necessary to roll back to a target historical version, the system can accurately filter out all memory records corresponding to the target version based on the version chain. This ensures that the memory state activated by the rollback operation is semantically completely consistent with the target version specified by the user, thereby avoiding memory errors or incomplete recovery caused by ambiguous positioning. Thus, while ensuring rollback speed, the system achieves both accuracy and reliability of the rollback results.
[0023] This specification provides one or more embodiments of an AI intelligent agent memory management device based on a PostgreSQL database, characterized in that it includes: At least one processor and bus; and, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enable the at least one processor to: A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions. When a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent's memory; A logical replication slot is created based on the logical decoding function of the relational database, and the logical replication slot is used to continuously capture data changes in the memory table to obtain change events; The change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier, and complete row data after the change. The structured information is then persistently stored in the audit log table. When the AI intelligent agent starts executing a specified task, a specified transaction snapshot identifier is exported from the relational database. During the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction to ensure that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task. Based on the version chain information in the audit log table and / or the memory table, perform backtracking and / or rollback operations on the memory state.
[0024] The above-described at least one technical solution adopted in the embodiments of this specification can achieve the following beneficial effects: This application sets a current validity flag and version chain information in the memory table, enabling each memory change to be persistently stored as an independent new record, effectively solving the technical problem of historical memory state loss in traditional solutions. Simultaneously, this application utilizes the logical decoding function of a relational database to create logical replication slots, and through an independent consumer service, parses captured change events into structured information and stores it in the audit log table, providing accurate time information for subsequent state backtracking. Furthermore, when the AI intelligent agent begins executing a specified task (such as a complex multi-step task), a specified transaction snapshot identifier can be exported from the database and imported into the new transaction before each memory query during task execution, ensuring that the agent's memory state remains consistent with the task's start time throughout the task's lifecycle. Finally, by combining the structured change records in the audit log table with the version chain information in the memory table, the complete memory at any historical moment can be reconstructed, enabling the reproduction of fault scenarios, or, through batch updating of the current validity flag, safely rolling back the entire memory system to a specified historical version. Attached Figure Description
[0025] To more clearly illustrate the technical solutions in the embodiments or prior art of this specification, the drawings used in the description of the embodiments or prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this specification. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. In the drawings: Figure 1 A flowchart illustrating an AI intelligent agent memory management method based on a PostgreSQL database, provided for one or more embodiments of this specification; Figure 2 This is a schematic diagram of the structure of an AI intelligent agent memory management device based on a PostgreSQL database, provided for one or more embodiments of this specification. Detailed Implementation
[0026] This specification provides an AI intelligent agent memory management method and device based on a PostgreSQL database.
[0027] To enable those skilled in the art to better understand the technical solutions in this specification, the technical solutions in the embodiments of this specification will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this specification, and not all embodiments. Based on the embodiments of this specification, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this specification.
[0028] Figure 1 This diagram illustrates a flowchart of an AI-powered intelligent agent memory management method based on a PostgreSQL database, provided for one or more embodiments of this specification. This process can be executed by an AI-powered intelligent agent memory management system. Certain input parameters or intermediate results in the process can be manually adjusted to help improve accuracy.
[0029] The method flow steps of the embodiments in this specification are as follows: S101, A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions.
[0030] In the embodiments described in this specification, a memory table can be created in a PostgreSQL relational database specifically for storing the memories of AI intelligent agents. Each record in this table contains at least the following fields: memory content, corresponding vector embedding, memory type, and currently valid flags for version management, version number, and parent version identifier field.
[0031] The current valid flag can be used to mark which memory record is currently the valid version that provides services to the outside world. The version number can be used to identify the order of different versions of the same memory entity. The parent version identifier field can point to the record identifier of the previous version from which the current record originates.
[0032] The version number and the parent version identifier field together form the version chain information, which allows each memory record to be traced back to its predecessor version along the parent version identifier, thereby constructing a complete version tracing path.
[0033] S102, when a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent memory.
[0034] In the embodiments described in this specification, when the system receives a memory change request from the AI intelligent agent, it first determines whether the request is a new memory request or an update memory request. If the request does not contain a memory identifier, it is determined to be a new memory request. In this case, a new record can be directly inserted into the memory table, a unique memory identifier is automatically generated for the record, the version number is set to the initial value, the parent version identifier field remains null, and the current validity flag is set to valid. If the request contains a memory identifier and a corresponding record exists in the memory table, it is determined to be an update memory request. In this case, the system starts a database transaction. Within this transaction, the current validity flag of the original record is first updated to invalid, then the version number of the original record is queried and incremented by one to obtain the new version number. Next, a new record is inserted, the memory content of the new record is the updated data, the memory identifier of the new record is automatically generated by the system, the version number is set to the calculated new version number, the parent version identifier field points to the identifier of the original record, the current validity flag is set to valid, and finally the transaction is committed.
[0035] In this way, all update operations are completed by inserting new records rather than overwriting old records. The old version data is fully preserved, and a traceable version chain is formed through the parent version identifier and version number.
[0036] S103, create a logical replication slot based on the logical decoding function of the relational database, and use the logical replication slot to continuously capture data changes in the memory table, thereby obtaining change events.
[0037] In the embodiments described in this specification, the system can create a dedicated logical replication slot in the database instance based on the logical decoding function of the PostgreSQL database. This replication slot is configured to continuously capture all data change operations that occur on the memory table. Once a new record is inserted into the memory table, the database kernel can retain the change event in the replication slot through the logical replication mechanism, preventing it from being prematurely removed by the regular log cleanup process.
[0038] S104, the change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier and complete row data after the change, and the structured information is persistently stored in the audit log table.
[0039] In the embodiments described in this specification, the system can deploy a background consumer service independent of the main application. Upon startup, this service can actively connect to the logical replication slot created in the previous step and enter a continuous listening state. Whenever a new change event occurs in the logical replication slot, the consumer service immediately receives the event, parses it, and extracts the operation type, the timestamp of the change, the identifier of the involved memory record, and the complete row data after the change. The consumer service assembles this extracted information into a structured record and inserts it into a separate audit log table. Each record in the audit log table contains an event identifier, event time, operation type, memory identifier, and complete row data stored in a structured format, thus forming an immutable, time-ordered memory change history.
[0040] S105, when the AI intelligent agent starts executing the specified task, the specified transaction snapshot identifier is exported from the relational database; during the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction so that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task.
[0041] In the embodiments described in this specification, when the AI intelligent agent needs to begin executing a complex task involving multiple rounds of interaction or multiple tool calls, it can first send a request to the memory management system to lock the current memory context. The memory management system initiates a brief database transaction, within which it calls the database's snapshot export function. This function returns a unique identifier that encodes the state of all active transactions in the database at the time of export.
[0042] The system submits the transaction and returns the obtained snapshot identifier to the AI intelligent agent. In each execution step of subsequent complex tasks, whenever the agent needs to query the memory, it passes the snapshot identifier along with the query request to the memory management system. Upon receiving the query request, the memory management system initiates a new database transaction, imports the previously saved snapshot identifier at the beginning of the transaction, performs the actual memory retrieval operation, and finally returns the query results.
[0043] Because the same snapshot identifier is imported, no matter how many new memory changes are committed in the database during task execution, the data seen by all query transactions is always completely consistent with the data at the start of the task, thus ensuring that the agent faces a static and globally consistent memory view throughout the entire task lifecycle.
[0044] S106, Perform backtracking and / or rollback operations on the memory state based on the version chain information in the audit log table and / or the memory table.
[0045] In the embodiments of this specification, for the retrospective of memory states, the system can query the audit log table based on a specified historical time point, filter out all change events whose event time was before that time point, and then replay these change operations sequentially according to the order in which the events occurred, thereby reconstructing the complete memory table state at that historical time point in memory or a temporary table. If it is also necessary to restore the actual perception at the historical decision-making moment, the system further obtains a specific transaction snapshot identifier exported by the agent near that time point, imports the snapshot identifier into an isolated database environment, and loads the aforementioned reconstructed memory state, thus accurately restoring the consistent memory view on which the restoration was based at that time.
[0046] In the embodiments described in this specification, for rolling back a memory state, the target historical version to which the rollback needs to be performed can be identified first. Then, a pre-defined rollback script is executed. This script starts a database transaction, which batch updates all records in the memory table whose current valid flag is valid to invalid within the transaction. Next, based on the version chain formed by the version number and the parent version identifier field, it accurately locates all memory records whose version numbers correspond to the target historical version, updates the current valid flag of these records to valid, and finally commits the transaction. The entire rollback process is completed atomically, and the system instantly switches to the memory state of the target historical version.
[0047] It should be noted that this application sets a current validity flag and version chain information in the memory table, enabling each memory change to be persistently stored as an independent new record, effectively solving the technical problem of historical memory state loss in traditional solutions. Simultaneously, this application utilizes the logical decoding function of a relational database to create a logical replication slot, and through an independent consumer service, parses captured change events into structured information and stores it in the audit log table, providing accurate time information for subsequent state backtracking. Furthermore, when the AI intelligent agent begins executing a specified task (such as a complex multi-step task), a specified transaction snapshot identifier can be exported from the database and imported into the new transaction before each memory query during task execution, ensuring that the agent's memory state remains consistent with the task's start time throughout the task's lifecycle. Finally, by combining the structured change records in the audit log table with the version chain information in the memory table, the complete memory at any historical moment can be reconstructed, enabling the reproduction of fault scenarios, or, by batch updating the current validity flag, safely rolling back the entire memory system to a specified historical version.
[0048] Furthermore, the version chain information includes a version number field and a parent version identifier field; wherein, the version number field is used to identify the version order of the memory record, and the parent version identifier field is used to point to the record identifier of the previous version from which the memory record originates.
[0049] It should be noted that the version number field in this application can represent the chronological order between different versions, and the parent version identifier field can represent the relationship between the current version and the previous version. This allows each newly created memory record to be traced back to the previous version from which it originated. When historical state reconstruction or fault reproduction is required, it is no longer necessary to rely on external logs; instead, the search can be performed directly along the parent version identifier field, while the correctness of the order can be confirmed based on the version number field.
[0050] Furthermore, in the process of creating a new record containing the current valid flag and the version chain information in the memory table through a database transaction when a memory change request is received, the method flow steps of this embodiment are as follows: S201, when the memory change request is a new memory request, insert a new record into the memory table, set an initial version number for the new record, and set the parent version identifier field of the new record to be null.
[0051] In the embodiments described in this specification, when a memory change request sent by the AI intelligent agent does not contain a memory identifier, the memory management system can determine that the request is a new memory request. The system then initiates a database transaction, within which a new memory record is constructed. The system automatically generates a globally unique memory identifier for this record, which is used for subsequent version tracking and association queries.
[0052] The memory content, vector embedding, and memory type of the new record are all filled in according to the values provided in the request. For version management related fields, the system sets the current validity flag to a valid state, indicating that the record is the current version being served externally. The version number field is set to the initial value, indicating that this is the first version of the memory entity. The parent version identifier field remains empty, indicating that the record has no predecessor version and is the starting node of the entire version chain.
[0053] It's important to note that when a memory change request is determined to be a new memory request, a new record is directly inserted into the memory table, with an initial version number set and the parent version identifier field left null. This indicates that the new memory is the starting node of the entire version chain, with no preceding versions to trace, thus avoiding redundant steps such as freezing old versions and querying parent version numbers for the new operation. Furthermore, this operation can be completed with a single insert statement within a single database transaction, ensuring that the process of writing new memories remains within the same data model as the existing versioned update mechanism.
[0054] Furthermore, when the memory change request is a memory update request, the method flow steps of this embodiment are as follows: S301, In a database transaction, update the current valid flag of the original record to be updated in the memory table to an invalid state.
[0055] In the embodiments described in this specification, when a memory change request sent by the AI intelligent agent contains a valid memory identifier, and the identifier has a corresponding record in the memory table, the system can determine that the request is a memory update request. The system can first start a database transaction, within which it locates the original record to be updated. The system modifies the current validity flag in the original record from a valid state to an invalid state. After this operation is completed, the original record can be logically frozen and no longer recognized by regular memory retrieval, but its complete data content and version chain information are still retained in the memory table for subsequent historical tracing and rollback.
[0056] S302, in the database transaction, a new record is inserted into the memory table. The content of the new record is the updated data. The version number of the new record is generated by incrementing the version number of the original record. The parent version identifier field of the new record points to the identifier of the original record. The current validity flag of the new record is set to a valid state.
[0057] In the embodiments described in this specification, within the same database transaction, the system queries the version number field of the original record to obtain its current version value, and increments this value by one unit to obtain the new version number. The system can automatically generate a unique memory identifier as the primary key of the new record, fill in the memory content, vector embedding, and memory type of the new record according to the updated data provided in the request, set the version number field to the calculated new version number, set the parent version identifier field to the identifier of the original record, so that the new record can point to its predecessor version through this field, set the current validity flag to a valid state, and mark the new record as the latest version currently being served. After completing the field assignment, the system performs an insert operation to write this new record into the memory table. Finally, the system commits the database transaction.
[0058] It should be noted that when an update memory request is received, this application updates the current validity flag of the original record to an invalid state, and then inserts a new record. The content of this new record is the updated data, and its version number is generated by incrementing the version number of the original record. Furthermore, its parent version identifier field points to the identifier of the original record. This ensures that the entire update process either succeeds entirely or is rolled back entirely. If it is necessary to locate the current valid memory later, it can be quickly retrieved by querying the record where the current validity flag is true.
[0059] Furthermore, in the process of exporting the specified transaction snapshot identifier from the relational database, the method flow steps of this embodiment are as follows: S401, the specified transaction snapshot identifier is obtained by calling the snapshot export function of the relational database.
[0060] In the embodiments described in this specification, when the AI intelligent agent needs to execute a complex task involving multiple rounds of interaction or multiple tool calls, it can first send a request to the memory management system to lock the current memory context. The memory management system can respond to this request and initiate a short-lived database transaction. Within this transaction, the system calls the snapshot export function built into the relational database. After the function executes, the database kernel records the state of all active transactions at the current moment and generates a unique identifier to return to the caller. This identifier specifies the transaction snapshot identifier, which encodes a globally consistent view of the database at the export moment. After obtaining the snapshot identifier, the memory management system commits the current transaction and returns the snapshot identifier to the AI intelligent agent. In subsequent steps of task execution, the agent will use the snapshot identifier to ensure that all memory queries are based on the same static and consistent data view.
[0061] It should be noted that this application can directly call the built-in snapshot export function of the relational database to obtain the snapshot identifier of the specified transaction, which can avoid state deviation caused by external intervention, and thus provide a stable and reliable memory context for AI intelligent agents during the execution of complex multi-step tasks.
[0062] Furthermore, during the process of backtracking the memory state, the method flow steps of this embodiment are as follows: S501, Based on the specified historical time point, query the audit log table, and reconstruct the complete memory table state of the specified historical time point by replaying the change events that occurred before the specified historical time point.
[0063] In the embodiments described in this specification, during the process of backtracking the memory state, the system first determines the specific time point of the historical moment, then queries the audit log table to filter out all change event records whose event time is less than or equal to the specified historical time point. These change events are arranged in chronological order, and each record contains the operation type, occurrence time, involved memory identifier, and the complete row data after the change. The system reads these change events one by one in chronological order and loads or updates the corresponding changed memory record for each event into a temporarily constructed memory state set. For insertion events, the system adds the memory record to the set; for update events, the system replaces the corresponding old version record in the set with the new version record. After all change events before the specified time point have been replayed, the memory state set obtained by the system accurately corresponds to the complete memory table state at the specified historical time point. This reconstruction process does not depend on whether the original memory table still retains historical version records; it is completed independently based entirely on the persistent structured change history in the audit log table.
[0064] It should be noted that this application can query the audit log table based on a specified historical time point, filter out all change events that occurred before that time point, and thus reconstruct the complete memory table state at that moment. Because the audit log table persistently stores structured change events, each event contains operation type, change time, and complete row data, it is possible to accurately restore a memory snapshot of any historical moment in a deterministic manner, independent of the current physical storage state of the database.
[0065] Furthermore, in the process of obtaining the specific transaction snapshot identifier exported by the AI intelligent agent near the specified historical time point, the method flow steps of this embodiment are as follows: S601, Import the specific transaction snapshot identifier into an isolated database transaction to reproduce the consistent memory state view on which the AI intelligent agent made decisions at the historical point in time.
[0066] In the embodiments described in this specification, after reconstructing the complete memory table state at a specified historical point in time, if it is still necessary to reproduce the memory view actually perceived by the intelligent agent when making decisions near that point in time, the system can further obtain the specific transaction snapshot identifier exported by the agent at that time. The system starts a new database transaction in an isolated database environment (e.g., a temporarily created test database or an independent database session). At the beginning of this transaction, the system calls the database's snapshot import function to import the previously obtained specific transaction snapshot identifier into the current transaction.
[0067] After the import operation is complete, the visibility rules of the current transaction are set to be completely consistent with the snapshot identifier export time. Then, the system can load the previously reconstructed historical point-in-time memory table state into this isolated environment. At this point, the memory view presented within the isolated transaction not only matches the historical point-in-time state in terms of data content, but also replicates the global consistency boundary seen by the agent at the time at the transaction isolation level.
[0068] It should be noted that, in addition to reconstructing the memory table state at a specified historical point in time, this application also needs to obtain specific transaction snapshot identifiers exported by the AI intelligent agent near that point in time and import these identifiers into an isolated database transaction. Therefore, the obtained memory view not only contains the data content at the historical point in time but also replicates the data consistency boundaries actually seen by the agent at that time. This is because the transaction snapshot identifier encodes the precise state of all active transactions in the database at the time of export. After importing, the visibility rules of all queries are set to be exactly the same as at the time of the agent's decision, thereby enabling the complete replication of the consistent memory context on which the agent made its decisions in an isolated environment.
[0069] Furthermore, during the rollback of the memorized state, the method flow steps in the embodiments of this specification are as follows: S701, determine the target historical version to which you need to roll back.
[0070] In the embodiments described in this specification, a known, well-preserved historical version can be identified as the rollback target. This target version can be confirmed based on records in the audit log table, version chain information, or business-level markers.
[0071] S702, in the database transaction, update all records in the memory table whose current valid flag is valid to invalid, and update the current valid flag of the memory records belonging to the target historical version to valid.
[0072] In the embodiments described in this specification, a pre-set rollback script is executed. This script can initiate a database transaction, first updating all records in the memory table whose current valid flags are in a valid state to an invalid state in batches, thereby instantly invalidating all memories currently providing external services. Then, based on the version number of the target historical version, combined with the version number field and the parent version identifier field in the version chain, all memory records belonging to the target historical version are precisely located, and the current valid flags of these records are updated to a valid state.
[0073] It should be noted that this application first sets all records with currently valid flags to invalid, and then sets the records corresponding to the target historical version to valid. Since the entire process is atomic, it can effectively prevent inconsistencies caused by mixing old and new version states during rollback. Furthermore, this operation only modifies the flag field, thus enabling the switching of the global memory state with minimal write overhead.
[0074] Furthermore, in the process of updating the currently valid flag bit of the memory record belonging to the target historical version to be valid, the method flow steps of this embodiment are as follows: S801, based on the version chain formed by the version number field and / or the parent version identifier field, locate all memory records whose version numbers correspond to the target historical version, and update the current valid flag of these records to valid.
[0075] In the embodiments described in this specification, within the database transaction where the rollback operation takes place, the system can first obtain the target historical version number specified by the administrator. Subsequently, the system can accurately locate the record based on the version chain information in the memory table. It can use the version number field to filter out all memory records whose version values are equal to the target historical version number. For cases where the version numbers are the same but there may be multiple branches, the system can further combine the tracing relationship formed by the parent version identifier field to confirm whether these records belong to a complete set on the same valid version chain. Through the above filtering, the system can identify all memory records belonging to the target historical version. After location is complete, the system can update the current validity flag of these records from an invalid state to a valid state, making each memory record in the target historical version a currently valid memory for external services again, thereby completing the activation step in the version rollback.
[0076] It should be noted that when it is necessary to roll back to a target historical version, the system can accurately filter out all memory records corresponding to the target version based on the version chain. This ensures that the memory state activated by the rollback operation is semantically completely consistent with the target version specified by the user, thereby avoiding memory errors or incomplete recovery caused by ambiguous positioning. Thus, while ensuring rollback speed, the system achieves both accuracy and reliability of the rollback results.
[0077] Furthermore, the structured information may also include a unique event identifier, and the operation types include insert operations and update operations.
[0078] It should be noted that when an independent consumer service parses change events obtained from the logical replication slot, a globally unique identifier can be generated for each change event as its identity. This unique event identifier can be generated using a method that has randomness and a low collision probability. When a new record is added to the memory table corresponding to a change event pushed by the logical replication slot, the consumer service can mark the operation type as an insert operation; when a new record is added to the memory table corresponding to a change event as an update result through a versioned write process, the consumer service can mark the operation type as an update operation.
[0079] Furthermore, the relational database can be a PostgreSQL database, and the logical decoding function can be the logical decoding function of PostgreSQL, which captures data changes of the memory table by creating logical replication slots.
[0080] It's worth noting that PostgreSQL can be chosen as the underlying relational database, with the pgvector extension installed to support vector embedding storage and retrieval. During initial deployment, the database administrator executes a logical replication slot creation command in the PostgreSQL instance, configuring a dedicated logical replication slot for this memory management system. This replication slot, based on PostgreSQL's built-in logical decoding function, is configured to continuously capture all data change operations occurring on the memory tables. The logical decoding function parses the insertion behavior of each record in the memory table into logical change events and writes these events sequentially into the replication slot. These events are retained until they are acknowledged for consumption by the consumer service, preventing them from being prematurely removed by the database's regular checkpointing or log cleanup mechanisms.
[0081] Figure 2 A schematic diagram of the structure of an AI intelligent agent memory management device based on a PostgreSQL database, provided for one or more embodiments of this specification, characterized in that it includes: At least one processor and bus; and, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enable the at least one processor to: A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions. When a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent's memory; A logical replication slot is created based on the logical decoding function of the relational database, and the data changes of the memory table are continuously captured using the logical replication slot to obtain change events; The change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier, and complete row data after the change. The structured information is then persistently stored in the audit log table. When the AI intelligent agent starts executing a specified task, a specified transaction snapshot identifier is exported from the relational database. During the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction to ensure that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task. Based on the version chain information in the audit log table and / or the memory table, perform backtracking and / or rollback operations on the memory state.
[0082] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the embodiments of apparatus, devices, and non-volatile computer storage media are basically similar to the method embodiments, so the descriptions are relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0083] The various embodiments in this specification are described in a progressive manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0084] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0085] In the embodiments provided in this application, it should be understood that the disclosed apparatus / network devices and methods can be implemented in other ways. For example, the apparatus / network device embodiments described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0086] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0087] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The aforementioned units can be implemented in hardware or software.
[0088] If the integrated module / unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording media, USB flash drives, portable hard drives, magnetic disks, optical disks, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the content included in the computer-readable medium can be appropriately added or removed according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electrical carrier signals and telecommunication signals.
[0089] The above-described embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application, and should all be included within the protection scope of this application.
Claims
1. A method for AI-powered intelligent agent memory management based on a PostgreSQL database, characterized in that, include: A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions. When a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent's memory; A logical replication slot is created based on the logical decoding function of the relational database, and the logical replication slot is used to continuously capture data changes in the memory table to obtain change events; The change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier, and complete row data after the change. The structured information is then persistently stored in the audit log table. When the AI intelligent agent starts executing a specified task, a specified transaction snapshot identifier is exported from the relational database. During the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction to ensure that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task. Based on the version chain information in the audit log table and / or the memory table, perform backtracking and / or rollback operations on the memory state.
2. The method according to claim 1, characterized in that, The version chain information includes a version number field and a parent version identifier field; wherein, the version number field is used to identify the version order of the memory record, and the parent version identifier field is used to point to the record identifier of the previous version from which the memory record originates.
3. The method according to claim 2, characterized in that, When a memory change request is received, creating a new record in the memory table containing the current valid flag and the version chain information through a database transaction includes: When the memory change request is a new memory request, a new record is inserted into the memory table, and an initial version number is set for the new record, and the parent version identifier field of the new record is empty.
4. The method according to claim 3, characterized in that, When the memory change request is a memory update request, the method further includes: In a database transaction, the current valid flag of the original record to be updated in the memory table is updated to an invalid state; In the database transaction, a new record is inserted into the memory table. The content of the new record is the updated data. The version number of the new record is generated by incrementing the version number of the original record. The parent version identifier field of the new record points to the identifier of the original record, and the current validity flag of the new record is set to a valid state.
5. The method according to claim 1, characterized in that, The step of exporting a specified transaction snapshot identifier from the relational database includes: The snapshot identifier of the specified transaction is obtained by calling the snapshot export function of the relational database.
6. The method according to claim 1, characterized in that, The process of backtracking the memory state includes: Based on a specified historical time point, query the audit log table, and reconstruct the complete memory table state at the specified historical time point by replaying the change events that occurred before the specified historical time point.
7. The method according to claim 6, characterized in that, The method further includes: When a specific transaction snapshot identifier derived by the AI intelligent agent near the specified historical time point is obtained, the specific transaction snapshot identifier is imported into an isolated database transaction to reproduce the consistent memory state view on which the AI intelligent agent made decisions at the historical time point.
8. The method according to claim 2, characterized in that, Perform a rollback on the memorized state, including: Determine the target historical version to which you need to roll back; In a database transaction, all records in the memory table whose current valid flag is valid are updated to invalid, and the current valid flag of the memory records belonging to the target historical version is updated to valid.
9. The method according to claim 8, characterized in that, The step of updating the current valid flag of the memory record belonging to the target historical version to valid includes: Based on the version chain formed by the version number field and / or the parent version identifier field, locate all memory records whose version numbers correspond to the target historical version, and update the current validity flag of these records to valid.
10. An AI-powered intelligent agent memory management device based on a PostgreSQL database, characterized in that: include: At least one processor and bus; as well as, A memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, which, when executed by the at least one processor, enable the at least one processor to: A memory table is established in a relational database to store the memories of AI intelligent agents. Each memory record in the memory table contains a current validity flag and version chain information. The current validity flag is used to identify the currently valid memory, and the version chain information is used to construct a traceability chain of historical versions. When a memory change request is received, a new record containing the current valid flag and the version chain information is created in the memory table through a database transaction, thereby realizing versioned storage of the AI intelligent agent's memory; A logical replication slot is created based on the logical decoding function of the relational database, and the logical replication slot is used to continuously capture data changes in the memory table to obtain change events; The change event is parsed through an independent consumer service to generate structured information, which includes operation type, change time, memory record identifier, and complete row data after the change. The structured information is then persistently stored in the audit log table. When the AI intelligent agent starts executing a specified task, a specified transaction snapshot identifier is exported from the relational database. During the execution of the specified task, whenever the AI intelligent agent needs to query the memory, the specified transaction snapshot identifier is imported into a new database transaction to ensure that the memory state on which the memory query is based is consistent with the memory state at the start of the specified task. Based on the version chain information in the audit log table and / or the memory table, perform backtracking and / or rollback operations on the memory state.