[5.7] Fix database cache on PostgreSQL #25530
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Storing serialized objects in the database cache doesn't work on PostgreSQL:
Retrieving the value throws an exception:
The database cache uses
serialize()
to serialize the value. For objects, the serialization contains null bytes. PostgreSQL can't store null bytes in a text column, truncates the string (O:8:"App\User":27:{s:11:"
) and thereby breaks the unserialization.The proposed solution is using Base64 encoding if the serialized value contains a null byte. When the value is retrieved, we can detect the Base64 encoding by looking for
:
or;
. A Base64-encoded string can't contain this characters; a serialized string always contains at least one of them.I used this to find the possible data type serializations:
This would only be a breaking change if someone was actually using the corrupted serialization – which makes no sense.
An alternative approach is storing the value in a binary column (
bytea
). This would also require changes to theDatabaseStore
and the user would have to adjust the migration. With the Base64 encoding, the user doesn't have to worry about the used database. The only downside is the increased payload size.Fixes #25466.