Skip to content

Investigate one-pass parameter writing #1727

@roji

Description

@roji

When writing protocol messages - and especially parameters within the Bind message -Npgsql currently performs two passes: one to validate all parameters and calculate the total byte length of the message, and another for actual writing.

Length calculation is needed because the PostgreSQL protocol requires the length up-front, but messages may not fit in the write buffer. Validation happens up-front because if we start writing anything before everything is validated, a validation error would leave us in an unrecoverable state where some protocol messages have been sent to PostgreSQL, and the connection would have to be broken.

It can be argued that validation errors are quite rare, and detecting them isn't extremely important - having a broken connection in case of a validation error could be acceptable. Also, in the vast majority of cases messages probably do fit in the write buffer.

So, we can implement a fast-path where the message is written in one pass, unless it doesn't fit in the buffer; if the latter scenario occurs, we switch back to a two-pass system. The performance impact of this needs to be studied, since the implementation of the two different paths is non-trivial.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions