Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: reset conn buffers after each query
There are two buffers in `pgwire.conn` that are used when sending results to the client. They have to buffer each row in its entirety before flushing, so wide rows can cause the capacity of these buffers to exceed the limit. Previously, the buffers were never reallocated even when they exceeded the size limit. This could cause a long-running session to hold on to large amounts of memory until the session was ended. This increased the risk of OOM, in addition to being inefficient. This commit adds a method `maybeReallocate` to `pgwire.conn`, which reallocates each buffer if it has reached the size limit. `maybeReallocate` is called upon closing `Sync` and `Flush` command results. These are natural places to reallocate the buffers, since they already flush the buffers to the client and are called roughly at the statement granularity, rather than for every row. This allows long-running sessions to bound their memory usage without causing reallocation on every wide row that is sent to the client. Informs cockroachdb#80497 Release note (performance improvement): Long-running sql sessions are now less likely to maintain large allocations for long periods of time, which decreases the risk of OOM and improves memory utilization.
- Loading branch information