Skip to content

Use sequence_id for JDBC chat memory ordering#6284

Closed
sobychacko wants to merge 2 commits into
spring-projects:mainfrom
sobychacko:GH-4740
Closed

Use sequence_id for JDBC chat memory ordering#6284
sobychacko wants to merge 2 commits into
spring-projects:mainfrom
sobychacko:GH-4740

Conversation

@sobychacko
Copy link
Copy Markdown
Contributor

Fixes #4740

The timestamp column in the JDBC chat memory schema never held temporal data; it only preserved message order within a conversation. Typing it as TIMESTAMP tied ordering to each database's timestamp precision, which scrambled message order on MySQL and MariaDB, where the default second-level precision collapses same-second inserts to equal values.

Replace it with sequence_id BIGINT, storing each message's position in the conversation. saveAll() already deletes and reinserts the whole conversation per save, so the batch index is a stable, portable ordering key written via setLong(). Update the eight dialect schemas and SQL, adjust the tests, and document the migration in the upgrade notes.

This is a breaking schema change for existing JDBC chat memory tables.

@sobychacko sobychacko added this to the 2.0.0-RC1 milestone Jun 4, 2026
@sdeleuze sdeleuze changed the title GH-4740: Use sequence_id for JDBC chat memory ordering Use sequence_id for JDBC chat memory ordering Jun 4, 2026
@sdeleuze
Copy link
Copy Markdown
Contributor

sdeleuze commented Jun 4, 2026

@sobychacko FYI I removed the RC1 milestone from the issue and removed "GH-4740: " from the PR description to have clean release notes.

Copy link
Copy Markdown
Contributor

@sdeleuze sdeleuze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A discussed, I suggest to keep timestamp to expose it as a metadata and add the sequence ID as a new column, will be easier for migrating, and will allow users to display when the message has been created if needed.

Fixes spring-projects#4740

The timestamp column in the JDBC chat memory schema never held
temporal data; it only preserved message order within a
conversation. Typing it as TIMESTAMP tied ordering to each
database's timestamp precision, which scrambled message order on
MySQL and MariaDB, where the default second-level precision
collapses same-second inserts to equal values.

Replace it with sequence_id BIGINT, storing each message's position
in the conversation. saveAll() already deletes and reinserts the
whole conversation per save, so the batch index is a stable,
portable ordering key written via setLong(). Update the eight
dialect schemas and SQL, adjust the tests, and document the
migration in the upgrade notes.

This is a breaking schema change for existing JDBC chat memory
tables.

Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
Fixes spring-projects#5168

Ordering now uses the sequence_id column, so the timestamp column is
no longer required for ordering. Rather than drop it, keep it as the
message creation time and make it available to applications.

Read the timestamp into the message metadata under the
JdbcChatMemoryRepository.CONVERSATION_TS key as an Instant, so
applications can display when a message was created. The value is
preserved across saves: re-saving a conversation reuses the timestamp
carried in each existing message's metadata and stamps the current
time only for newly added messages.

Keeping the column also eases migration, since existing tables only
need the new sequence_id column added rather than a column replaced.

Because read-back messages now carry timestamp metadata, a retrieved
message is no longer equal to an otherwise-identical message
constructed in code; tests that compared messages by value now compare
by content.

Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
sdeleuze pushed a commit to sdeleuze/spring-ai that referenced this pull request Jun 4, 2026
The timestamp column in the JDBC chat memory schema never held
temporal data; it only preserved message order within a
conversation. Typing it as TIMESTAMP tied ordering to each
database's timestamp precision, which scrambled message order on
MySQL and MariaDB, where the default second-level precision
collapses same-second inserts to equal values.

Replace it with sequence_id BIGINT, storing each message's position
in the conversation. saveAll() already deletes and reinserts the
whole conversation per save, so the batch index is a stable,
portable ordering key written via setLong(). Update the eight
dialect schemas and SQL, adjust the tests, and document the
migration in the upgrade notes.

This is a breaking schema change for existing JDBC chat memory
tables.

Fixes spring-projects#4740
See spring-projects#6284
Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
@sdeleuze sdeleuze closed this in 08e13d7 Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace timestamp column with sequence_id in JDBC chat memory schema

2 participants