Skip to content

Allow writing parameters in text format (with a specific type OID) #4053

Description

@roji

In some scenarios, it may be useful for people to write text encoding of data, where they provide the string directly (see #4046 (comment) for an example).

Consider doing together with #4052, which is the same for binary.

Activity

  1. self-assigned this
    on Oct 18, 2021
  2. roji commented on Oct 25, 2021

    @roji
    MemberAuthor

    This is a bit less trivial than it looks:

    • We still need to resolve the real type handler (so that we send the correct type OID if NpgsqlDbType/DataTypeName are specified)
    • But we need to bypass validation entirely, and also do special TextHandler writing (not the handler's). We could possibly add checks in NpgsqlParameter to go through UnknownTypeHandler's validation/length calculation/writing, but it's annoying to add special logic in this hot path for something that's a very exotic edge case.

    Note that we already have the UnknownTypeHandler mechanis: if NpgsqlDbType is set to Unknown, Npgsql sends the user-provided string with type OID=0, and allows PG to infer the the type based on context. If the context is unambiguous (e.g. inserting into a table column), this should work just fine; the problem is only for ambiguous contexts.

    @marco6 can you please comment on whether this could work for you? If so, then you don't need anything at all from us: just read out text values (even using the existing API in Npgsql 5.0), shove them in NpgsqlParameter.Value and set the NpgsqlDbType to Unknown.

  3. changed the title [-]Allow writing parameters in text format[/-] [+]Allow writing parameters in text format (with a specific type OID)[/+] on Oct 27, 2021
  4. added this to the Backlog milestone on Oct 27, 2021
  5. marco6 commented on Oct 30, 2021

    @marco6
    Contributor

    I have tested NpgsqlDbType.Unknown and it seems working in my current use-case as I am inserting into tables/using parameters in where clauses, so Postgres can actually infer the type itself. I will keep you updated if something strange comes out. Thank you for your support for now! 😄

  6. roji commented on Oct 30, 2021

    @roji
    MemberAuthor

    Great - NpgsqlDbType.Unknown should indeed be a very reasonable solution for the majority of cases (especially those likely to be used with replication).

    I've put this (and #4052) in the backlog to revisit in the future if necessary.

  7. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions