F.14. dump_stat

The dump_stat module provides functions that allow you to backup and recover the contents of the pg_statistic table. The dump_statistic function generates INSERT statements which can later be applied to a compatible database. The extension should be installed on the recipient server in order to successfully restore statistical data since these statements heavily rely on the provided functions.

Note that the definition of the pg_statistic table might change occasionally, which means that generated dump might be incompatible with future releases of Postgres Pro.

F.14.1. Functions

dump_statistic() returns setof text

dump_statistic dumps the contents of the pg_statistic system catalog. It produces an INSERT statement per each tuple of the pg_statistic, excluding the ones that contain statistical data for tables in the information_schema and pg_catalog schemas.

The INSERT statement takes form of

WITH upsert as (
  UPDATE pg_catalog.pg_statistic SET column_name = expression [, ...]
  WHERE to_schema_qualified_relation(starelid) = t_relname
    AND to_attname(t_relname, staattnum) = t_attname
    AND to_atttype(t_relname, staattnum) = t_atttype
    AND stainherit = t_stainherit
  RETURNING *)
ins as (
  SELECT expression [, ...]
  WHERE NOT EXISTS (SELECT * FROM upsert)
    AND to_attnum(t_relname, t_attname) IS NOT NULL
    AND to_atttype(t_relname, t_attname) = t_atttype)
INSERT INTO pg_catalog.pg_statistic SELECT * FROM ins;

where expression can be one of:

array_in(array_text, type_name::regtype::oid, -1)
value::type_name

Produced statements could be saved to a file, e.g.

$ psql test -A
test=# \t
test=# \o dump_stat.sql
test=# select dump_statistic();

dump_statistic(schema_name text) returns setof text

dump_statistic dumps the contents of the pg_statistic system catalog. It produces an INSERT statement per each tuple of the pg_statistic that relates to some table in the schema_name schema.

dump_statistic(schema_name text, table_name text) returns setof text

dump_statistic dumps the contents of the pg_statistic system catalog. It produces an INSERT statement per each tuple of the pg_statistic that relates to the specified schema_name.table_name table.

dump_statistic(relid oid) returns setof text

dump_statistic dumps the contents of the pg_statistic system catalog. It produces an INSERT statement per each tuple of the pg_statistic that contains statistical data for the relation referenced by relid.

to_schema_qualified_operator(opid oid) returns text

Fetches the schema-qualified operator name by operator id opid. For example:

test=# SELECT to_schema_qualified_operator('+(int,int)'::regoperator);
          to_schema_qualified_operator          
------------------------------------------------
 pg_catalog.+(pg_catalog.int4, pg_catalog.int4)
(1 row)
to_schema_qualified_type(typid oid) returns text

Fetches the schema-qualified type name by type id typid.

to_schema_qualified_relation(relid oid) returns text

Fetches the schema-qualified relation name by relation id relid.

anyarray_elemtype(arr anyarray) returns oid

Returns the element type of the given array as oid. For example:

test=# SELECT anyarray_elemtype(array_in('{1,2,3}', 'int'::regtype, -1));
 anyarray_elemtype 
-------------------
                23
(1 row)
to_attname(relation text, colnum int2) returns text

Given a relation name relation and a column number colnum, returns the column name as text.

to_attnum(relation text, col text) returns int2

Given a relation name relation and a column name col, returns the column number as int2.

to_atttype(relation text, col text) returns text

Given a relation name relation and a column name col, returns the schema-qualified column type as text.

to_atttype(relation text, colnum int2) returns text

Given a relation name relation and a column number colnum, returns the schema-qualified column type as text.

to_namespace(nsp text) returns oid

to_namespace duplicates the behavior of the cast to the regnamespace type, which is not present in the PostgreSQL 9.4 release (and prior releases). This function returns the oid of the given schema.

get_namespace(relation oid) returns oid

get_namespace returns the schema of the given relation as oid.