Skip to content

Instantly share code, notes, and snippets.

@qdw
Created October 9, 2015 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qdw/00d9be4ce103377fe03c to your computer and use it in GitHub Desktop.
Save qdw/00d9be4ce103377fe03c to your computer and use it in GitHub Desktop.
This convenience function lets you query stats by table and column name (as opposed to using numeric IDs) (PostgreSQL, PL/pgSQL)
CREATE OR REPLACE FUNCTION stats(my_table text, my_column text) RETURNS SETOF pg_statistic AS $$
DECLARE
my_relid oid := (SELECT oid FROM pg_class WHERE relname = my_table);
my_attnum smallint := (SELECT attnum FROM pg_attribute WHERE attrelid = my_relid AND attname = my_column);
BEGIN
RETURN QUERY SELECT * FROM pg_statistic WHERE starelid = my_relid AND staattnum = my_attnum;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment