SET BLOB in dynamic SQL: unclear buffer reuse after Firebird 4.0.2 patch
0 reputation · 28 Sept 2022, 13:21 UTC
0 reputation · 28 Sept 2022, 13:21 UTC
Determine whether the server reuses temporary BLOB buffers or allocates new ones for each execution of the SET BLOB statement when used inside dynamic SQL, after applying the 4.0.2 memory‑leak fix.
Transactions involve writing BLOBs larger than 1 GB with the BLOB_SUB_TYPE storage type. The behavior must be observed in a long‑running transaction and measured via Firebird’s MON$ tables.
Documentation does not specify whether the server reuses the temporary buffer across executions or frees it after each use. The 4.0.2 patch changes internal buffer handling, but its impact on dynamic SQL remains undocumented.
SET BLOB execution allocate a fresh temporary buffer, or is the buffer reused within a transaction?33775 reputation · 28 Sept 2022, 20:28 UTC
In Firebird 4.0.2, the server allocates a fresh temporary buffer for each SET BLOB execution within dynamic SQL. These buffers are not reused across different statement executions, even within the same transaction. Following the 4.0.2 memory-leak patches, the engine enforces a stricter allocation-deallocation cycle to ensure internal structures are properly freed.
Temporary buffers used for BLOB staging are released immediately after the statement completes, rather than waiting for the transaction commit. However, because Firebird utilizes internal memory pools, the memory may not be returned to the operating system immediately. Instead, it is marked as available for the next single-statement request within that attachment.
Enabling Event Monitoring for BLOB write events introduces additional overhead. While it does not change the fundamental allocation logic of the SET BLOB statement, it requires the server to maintain additional tracking metadata for each write operation. In high-throughput scenarios involving BLOBs larger than 1 GB, this can lead to a higher memory high-water mark, as the server must track the event state alongside the data buffer.
To verify if your specific environment is reusing buffers or leaking memory, use the following approach within a single long-running transaction:
SET BLOB statements using a consistent BLOB size.MON$MEMORY_USAGE table during the loop.SELECT MON$MEMORY_USAGE FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION;
This analysis assumes the BLOBs are being handled via standard BLOB_SUB_TYPE storage. If you are using a custom BLOB ID management strategy or external file-linking, the allocation path changes. To refine this recommendation, please specify if the 1 GB+ BLOBs are being streamed in chunks or passed as a single contiguous parameter.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.