Skip to content

DatabaseStore crashes on PostgreSQL with OperationalError: string too long to represent as jsonb string #2207

@adamchainz

Description

@adamchainz

On a page with many database queries, the DatabaseStore managed to trigger:

File "/.../.venv/lib/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
  return self.cursor. execute(sql, params)
File "/.../.venv/lib/python3.13/site-packages/psycopg/cursor.py", line 97, in execute
  raise ex.with_traceback(None)
django.db.utils.OperationalError: string too long to represent as jsonb string
LINE 1: UPDATE "debug_toolbar_historyentry" SET "data" = E'{"CacheP...

DETAIL: Due to an implementation restriction, jsonb strings cannot exceed 268435455 bytes.

It seems that the design of storing everything in one JSON field will need to follow this restriction, perhaps through chunking or making the data structure smarter

Alternatively, and possibly more fittingly, avoid using JSONField. If the JSON data doesn't need deconstructing within the database for queries, it's better to store JSON data in a text field. This can be any size and then loaded in Python only. The data can also then be compressed with zlib in the database, to save disk space and make writes and reads faster. (Django-MySQL's cache backend does this, for example.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions