I’ve created a small helper library called
PostgrexTextExt. It allows users to enable text protocol encoding/decoding in Postgrex for any PostgeSQL data type they choose, even if Postgrex doesn’t currently support it.
I created it based on some edge cases I saw while helping with Ecto over the past year. In particular, it seems like it helps in the following cases:
Text search functions requiring OIDs: Error interpolating `ts_vector` Ecto - is this a bug?. It avoids the double casting
Extensions that don’t have a binary protocol version. For example,
ltreedidn’t have a binary protocol under somewhat recently
Using Postgrex directly instead of Ecto and preferring the text versions of certain types (i.e. UUID): pass uuid as string by OleMchls · Pull Request #271 · elixir-ecto/postgrex · GitHub
I started off wanting to make an extension just for OIDs, but then I realized with very little effort you can make it work for any type.
How it’s used:
- Specify the data types in your configuration
config :postgrex_text_ext, type_names: ["regconfig", "ltree"], type_outputs: ["range_out"]
There are 2 levels of granularity: type name and type output. These correspond to the
typoutput columns in the pg_type system catalog.
A list of type names and outputs is provided here. You may also use the
pg_type system catalog:
SELECT typname, typoutput FROM pg_type;.
- Define a custom type module, as needed by Postgrex
Postgrex.Types.define(MyApp.PostgrexTypes, [PostgrexTextExt], )