A PostgreSQL function that ports the spirit of SQL Server's sp_help stored procedure to Postgres. Returns a single result set describing a table's columns, constraints, and indexes.
In SQL Server, sp_help 'mytable' is the quickest way to get a summary of a table's structure. Postgres has \d tablename in psql, but that's a client command — you can't call it from a GUI, a notebook, an application, or anywhere else that just speaks SQL. pg_help fills that gap by returning the same kind of information as a regular result set.
Run the function definition against your database:
psql -d mydb -f pg_help.sqlPass the fully-qualified table name (schema.table) as a string:
select * from pg_help('public.users');Four varchar(500) columns (col1, col2, col3, col4) containing section headers and rows, in this order:
- Table — the table name
- Columns — column name, data type, nullability
- Constraints — primary keys, foreign keys, checks, uniques
- Indexes — index name, columns, optional
WHEREclause
PostgreSQL 9.x or later. Uses standard catalogs (pg_constraint, pg_index, pg_class, information_schema.columns) so no extensions are needed.
- The table name must be fully qualified (e.g.
public.users, not justusers). - Output columns are fixed-width
varchar(500), mimicking thesp_helpresult-grid style. - For interactive use in
psql,\d+ tablenameis usually a better choice.pg_helpis most useful when you need the metadata as a queryable result set.