Skip to content

Expose rich PostgreSQL data type information #1276

Closed

Description

@roji

Consumers of Npgsql sometimes need access to information on PostgreSQL data types. This is the case, for example, with the EFCore provider, which needs to know whether certain columns are of type array, as well as what their element type is.

Npgsql already has a rich representation of the backend's types internally. Expose this representation to the user via:

  • NpgsqlDataReader.GetColumnSchema
  • The upcoming ADO.NET database schema API
  • NpgsqlDataReader.GetPostgresType (similar to NpgsqlDataReader.GetDataTypeName)

Activity

  1. added this to the 3.2 milestone on Sep 11, 2016
  2. self-assigned this
    on Sep 11, 2016
  3. arjennienhuis commented on Sep 11, 2016

    @arjennienhuis
    Contributor

    Would that return a NpgsqlDbType? I needed that before for better BINARY COPY support.

  4. roji commented on Sep 12, 2016

    @roji
    MemberAuthor

    Yes, you would be to get a new PostgresType instance that represents the data type of a column, and retrieve its associated NpgsqlDbType.

    Out of curiosity, what was your scenario? When using binary copy, it's expected that you know the the data types of columns you read/write in advance...

  5. arjennienhuis commented on Sep 12, 2016

    @arjennienhuis
    Contributor

    It might be expected and I thought I knew the data types.

    The very first copy I tried I confused a varchar(4) with an integer. No warnings, no errors just 'different' values.

    Now I first do:

    SELECT {cols} FROM {table} LIMIT 0
    

    Then I do something silly to get the data types followed by:

    COPY {table} ({cols}) FROM STDIN ...
    
  6. roji commented on Sep 12, 2016

    @roji
    MemberAuthor

    Yeah, the COPY API requires you to know what you're doing. I actually thought about removing the option to read/write in the COPY api without explicitly specifying the NpgsqlDbType to make people think about this aspect.

    But once you're code is written and works, I can see little reason to inspect column metadata in runtime and to extract the NpgsqlDbType...

    BTW instead of querying with LIMIT 0 you can do ExecuteReader() with CommandBehavior.SchemaOnly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions