Search code examples
postgresqlclientprotocols

Dealing with parsing oids in Postgres


I'm currently improving a library client for Postgresql, the library already has working communication protocol including DataRow and RowDescription.

The problem I'm facing right now is how to deal with values. Returning plain string with array of integers for example is kind of pointless.

By my research I found that some other libraries (like for Python) either return is as unmodified string or convert primitive types including arrays. What I mean by conversion is making Postgres DataRow raw data as Python-type value: Postgres integer is parsed as python number, Postgres booleans as python booleans, etc.

Should I make second query to get information column type and use its converters or should I leave it plain?


Solution

  • You could opt to get the array values in the internal format by setting the corresponding "result-column format code" in the Bind message to 1, but that is typically a bad choice, since the internal format varies from type to type and may even depend on the server's architecture.

    So your best option is probably to parse the string representation of the array on the client side, including all the escape characters.

    When it comes to finding the base type for an array type, there is no other option than querying pg_type like

    SELECT typelem::regtype FROM pg_type WHERE oid = 1007;
    
     typelem 
    ---------
     integer
    (1 row)
    

    You could cache these values on the client side so that you don't have to query more than once per type and database session.