oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

Extracting pg_types from the system catalog #317

Open jeromew opened 1 year ago

jeromew commented 1 year ago

Here is a small contribution extracted from a project of mine in case you consider it can be useful for the pg-mem project.

The list of supported types in postgres when the catalog is created and their high-level definition is located in https://github.com/postgres/postgres/raw/master/src/include/catalog/pg_type.dat

you can recover a json file with the corresponding definitions with the following shell script

#!/bin/sh -x
# This script fetches description of types on the postgres master branch
# and transforms it into a json parsable file

wget https://github.com/postgres/postgres/raw/master/src/include/catalog/pg_type.dat

# remove lines beginning with #
sed -i '/^#/d' pg_type.dat
# make sure keys are single-quoted
sed -i 's/\([_a-z]\+\) =>/'\''\1'\'':/g' pg_type.dat
# replace all single-quotes by double-quotes
sed -i 's/'\''/'\"'/g' pg_type.dat
# remove the last comma in the file
tac pg_type.dat | awk '/,$/ && !handled { sub(/,$/, ""); handled++ } {print}' | tac > pg_type.json
rm pg_type.dat