Provide schema and count information in`Cursor`
helje5 opened this issue · 2 comments
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
.
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
).
Available in v1.2.0.