Skip to content
This repository was archived by the owner on Jun 19, 2026. It is now read-only.
This repository was archived by the owner on Jun 19, 2026. It is now read-only.

Provide schema and count information inCursor #12

Closed

Description

@helje5

It is often useful to have schema information for a fetch. That is for clients which allow the user or developer to enter arbitrary queries (to then reflect on results, e.g. to show them in a table w/ a proper column name header).

libpq provides this using the PQnfields function to get the number of columns returned, and then PQfname to extract the name associated with the column.

In addition libpq provides PQntuples which has the number of rows being returned in advance. That is also very useful (e.g. the preallocate result data structures with the proper size before fetching into them).

It's been a while since I looked at the raw protocol, but I all this information is readily available in the query response and doesn't require fetching all records.

https://www.postgresql.org/docs/11/libpq-exec.html#LIBPQ-EXEC-SELECT-INFO

The PostgresValue, could also carry the actual PG OID type (maybe as a nice enum) similar to PQftype.

Activity

  1. added 2 commits that reference this issue on May 28, 2020
  2. pitfield commented on May 28, 2020

    @pitfield
    Member

    Added a new parameter, retrieveColumnMetadata, to Statement.execute(...), defaulting to false. If true, result metadata is retrieved and can be accessed through the new columns property of Cursor. The result metadata includes the name and data type of each column in the results (plus some other stuff).

    As far as I know, the Postgres wire protocol does not allow the number of rows in a result to be retrieved before the rows are brought over the wire. Please let me know if you are aware of a way to do this. PQntuples is passed the PGresult, so I think by that point the rows are already in memory. PostgresClientKit, on the other hand, (intentionally) lazily retrieves the rows over the wire, so the number of rows is known only once they have been retrieved (see Cursor.rowCount).

  3. pitfield commented on May 29, 2020

    @pitfield
    Member

    Available in v1.2.0.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions