Skip to content
This repository was archived by the owner on May 25, 2026. It is now read-only.

fix(db): change state_data column from jsonb to text to fix 22P02 error - #9

Merged
cherninkiy merged 1 commit into
mainfrom
dev
May 15, 2026
Merged

fix(db): change state_data column from jsonb to text to fix 22P02 error#9
cherninkiy merged 1 commit into
mainfrom
dev

Conversation

@cherninkiy

Copy link
Copy Markdown
Owner

No description provided.

@cherninkiy
cherninkiy merged commit 214c5c8 into main May 15, 2026
2 checks passed
@cherninkiy
cherninkiy deleted the dev branch May 15, 2026 10:54

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request changes the state_data column type from JSONB to TEXT in the database initialization script and the application's database context. Feedback highlights that the SQL change won't apply to existing databases due to the IF NOT EXISTS clause and may cause base64 decoding errors because existing JSON quotes are preserved during the type cast. Additionally, storing large binary data as Base64 in a text column is noted as inefficient, with suggestions to use BYTEA or external file storage instead.

Comment thread db/init.sql
document_id UUID NOT NULL,
current_activity VARCHAR(128) NOT NULL,
state_data JSONB,
state_data TEXT,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

Changing the column type in init.sql will not update existing databases where the table already exists due to the IF NOT EXISTS clause. Additionally, a simple type cast from JSONB to TEXT in an ALTER TABLE statement will preserve JSON quotes (e.g., storing "SGVsbG8=" instead of SGVsbG8=). This will cause Convert.FromBase64String to fail in DocumentProcessingAgent.cs (line 82) when the agent attempts to resume from a checkpoint created before this change.

Recommendation: Ensure a migration script is provided that both alters the type and unquotes existing data:

ALTER TABLE workflow_checkpoints ALTER COLUMN state_data TYPE TEXT USING state_data#>>'{}';

entity.Property(e => e.AgentName).HasMaxLength(128).IsRequired();
entity.Property(e => e.CurrentActivity).HasMaxLength(128).IsRequired();
entity.Property(e => e.StateData).HasColumnType("jsonb");
entity.Property(e => e.StateData).HasColumnType("text");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

While changing the column type to text resolves the 22P02 (invalid JSON) error, storing large binary data as Base64 in a database column is inefficient. Base64 encoding increases the payload size by approximately 33%, leading to significant database bloat and increased memory pressure when checkpoints are retrieved.

Since the DocumentId and FilePath are already available in the AgentContext, consider refactoring the agent to re-retrieve the file from IFileStorage upon resume instead of serializing the entire PDF into the database. If storing the payload is strictly necessary, a BYTEA column or a dedicated blob storage would be more performant.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant