Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
271 changes: 271 additions & 0 deletions doc/src/sgml/func.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -28025,6 +28025,277 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
</tgroup>
</table>

<para>
<xref linkend="functions-admin-statsmod"/> lists functions used to
manipulate statistics.
<warning>
<para>
Changes made by these statistics manipulation functions are likely to be
overwritten by <link linkend="autovacuum">autovacuum</link> (or manual
<command>VACUUM</command> or <command>ANALYZE</command>) and should be
considered temporary.
</para>
</warning>
</para>

<table id="functions-admin-statsmod">
<title>Database Object Statistics Manipulation Functions</title>
<tgroup cols="1">
<thead>
<row>
<entry role="func_table_entry"><para role="func_signature">
Function
</para>
<para>
Description
</para></entry>
</row>
</thead>

<tbody>
<row>
<entry role="func_table_entry">
<para role="func_signature">
<indexterm>
<primary>pg_set_relation_stats</primary>
</indexterm>
<function>pg_set_relation_stats</function> (
<parameter>relation</parameter> <type>regclass</type>
<optional>, <parameter>relpages</parameter> <type>integer</type></optional>
<optional>, <parameter>reltuples</parameter> <type>real</type></optional>
<optional>, <parameter>relallvisible</parameter> <type>integer</type></optional> )
<returnvalue>void</returnvalue>
</para>
<para>
Updates relation-level statistics for the given relation to the
specified values. The parameters correspond to columns in <link
linkend="catalog-pg-class"><structname>pg_class</structname></link>. Unspecified
or <literal>NULL</literal> values leave the setting unchanged.
</para>
<para>
Ordinarily, these statistics are collected automatically or updated
as a part of <xref linkend="sql-vacuum"/> or <xref
linkend="sql-analyze"/>, so it's not necessary to call this
function. However, it may be useful when testing the effects of
statistics on the planner to understand or anticipate plan changes.
</para>
<para>
The caller must have the <literal>MAINTAIN</literal> privilege on
the table or be the owner of the database.
</para>
<para>
The value of <structfield>relpages</structfield> must be greater than
or equal to <literal>-1</literal>,
<structfield>reltuples</structfield> must be greater than or equal to
<literal>-1.0</literal>, and <structfield>relallvisible</structfield>
must be greater than or equal to <literal>0</literal>.
</para>
</entry>
</row>

<row>
<entry role="func_table_entry">
<para role="func_signature">
<indexterm>
<primary>pg_clear_relation_stats</primary>
</indexterm>
<function>pg_clear_relation_stats</function> ( <parameter>relation</parameter> <type>regclass</type> )
<returnvalue>void</returnvalue>
</para>
<para>
Clears table-level statistics for the given relation, as though the
table was newly created.
</para>
<para>
The caller must have the <literal>MAINTAIN</literal> privilege on
the table or be the owner of the database.
</para>
</entry>
</row>

<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_restore_relation_stats</primary>
</indexterm>
<function>pg_restore_relation_stats</function> (
<literal>VARIADIC</literal> <parameter>kwargs</parameter> <type>"any"</type> )
<returnvalue>boolean</returnvalue>
</para>
<para>
Similar to <function>pg_set_relation_stats()</function>, but intended
for bulk restore of relation statistics. The tracked statistics may
change from version to version, so the primary purpose of this
function is to maintain a consistent function signature to avoid
errors when restoring statistics from previous versions.
</para>
<para>
To match the behavior of <xref linkend="sql-vacuum"/> and <xref
linkend="sql-analyze"/> when updating relation statistics,
<function>pg_restore_relation_stats()</function> does not follow MVCC
transactional semantics (see <xref linkend="mvcc"/>). New relation
statistics may be durable even if the transaction aborts, and the
changes are not isolated from other transactions.
</para>
<para>
Arguments are passed as pairs of <replaceable>argname</replaceable>
and <replaceable>argvalue</replaceable>, where
<replaceable>argname</replaceable> corresponds to a named argument in
<function>pg_set_relation_stats()</function> and
<replaceable>argvalue</replaceable> is of the corresponding type.
</para>
<para>
Additionally, this function supports argument name
<literal>version</literal> of type <type>integer</type>, which
specifies the version from which the statistics originated, improving
intepretation of older statistics.
</para>
<para>
For example, to set the <structname>relpages</structname> and
<structname>reltuples</structname> of the table
<structname>mytable</structname>:
<programlisting>
SELECT pg_restore_relation_stats(
'relation', 'mytable'::regclass,
'relpages', 173::integer,
'reltuples', 10000::float4);
</programlisting>
</para>
<para>
Minor errors are reported as a <literal>WARNING</literal> and
ignored, and remaining statistics will still be restored. If all
specified statistics are successfully restored, return
<literal>true</literal>, otherwise <literal>false</literal>.
</para>
</entry>
</row>

<row>
<entry role="func_table_entry">
<para role="func_signature">
<indexterm>
<primary>pg_set_attribute_stats</primary>
</indexterm>
<function>pg_set_attribute_stats</function> (
<parameter>relation</parameter> <type>regclass</type>,
<parameter>attname</parameter> <type>name</type>,
<parameter>inherited</parameter> <type>boolean</type>
<optional>, <parameter>null_frac</parameter> <type>real</type></optional>
<optional>, <parameter>avg_width</parameter> <type>integer</type></optional>
<optional>, <parameter>n_distinct</parameter> <type>real</type></optional>
<optional>, <parameter>most_common_vals</parameter> <type>text</type>, <parameter>most_common_freqs</parameter> <type>real[]</type> </optional>
<optional>, <parameter>histogram_bounds</parameter> <type>text</type> </optional>
<optional>, <parameter>correlation</parameter> <type>real</type> </optional>
<optional>, <parameter>most_common_elems</parameter> <type>text</type>, <parameter>most_common_elem_freqs</parameter> <type>real[]</type> </optional>
<optional>, <parameter>elem_count_histogram</parameter> <type>real[]</type> </optional>
<optional>, <parameter>range_length_histogram</parameter> <type>text</type> </optional>
<optional>, <parameter>range_empty_frac</parameter> <type>real</type> </optional>
<optional>, <parameter>range_bounds_histogram</parameter> <type>text</type> </optional> )
<returnvalue>void</returnvalue>
</para>
<para>
Creates or updates attribute-level statistics for the given relation
and attribute name to the specified values. The parameters correspond
to to attributes of the same name found in the <link
linkend="view-pg-stats"><structname>pg_stats</structname></link>
view.
</para>
<para>
Optional parameters default to <literal>NULL</literal>, which leave
the corresponding statistic unchanged.
</para>
<para>
Ordinarily, these statistics are collected automatically or updated
as a part of <xref linkend="sql-vacuum"/> or <xref
linkend="sql-analyze"/>, so it's not necessary to call this
function. However, it may be useful when testing the effects of
statistics on the planner to understand or anticipate plan changes.
</para>
<para>
The caller must have the <literal>MAINTAIN</literal> privilege on
the table or be the owner of the database.
</para>
</entry>
</row>

<row>
<entry role="func_table_entry">
<para role="func_signature">
<indexterm>
<primary>pg_clear_attribute_stats</primary>
</indexterm>
<function>pg_clear_attribute_stats</function> (
<parameter>relation</parameter> <type>regclass</type>,
<parameter>attname</parameter> <type>name</type>,
<parameter>inherited</parameter> <type>boolean</type> )
<returnvalue>boolean</returnvalue>
</para>
<para>
Clears table-level statistics for the given relation attribute, as
though the table was newly created.
</para>
<para>
The caller must have the <literal>MAINTAIN</literal> privilege on
the table or be the owner of the database.
</para>
</entry>
</row>

<row>
<entry role="func_table_entry"><para role="func_signature">
<indexterm>
<primary>pg_restore_attribute_stats</primary>
</indexterm>
<function>pg_restore_attribute_stats</function> (
<literal>VARIADIC</literal> <parameter>kwargs</parameter> <type>"any"</type> )
<returnvalue>boolean</returnvalue>
</para>
<para>
Similar to <function>pg_set_attribute_stats()</function>, but
intended for bulk restore of attribute statistics. The tracked
statistics may change from version to version, so the primary purpose
of this function is to maintain a consistent function signature to
avoid errors when restoring statistics from previous versions.
</para>
<para>
Arguments are passed as pairs of <replaceable>argname</replaceable>
and <replaceable>argvalue</replaceable>, where
<replaceable>argname</replaceable> corresponds to a named argument in
<function>pg_set_attribute_stats()</function> and
<replaceable>argvalue</replaceable> is of the corresponding type.
</para>
<para>
Additionally, this function supports argument name
<literal>version</literal> of type <type>integer</type>, which
specifies the version from which the statistics originated, improving
intepretation of older statistics.
</para>
<para>
For example, to set the <structname>avg_width</structname> and
<structname>null_frac</structname> for the attribute
<structname>col1</structname> of the table
<structname>mytable</structname>:
<programlisting>
SELECT pg_restore_attribute_stats(
'relation', 'mytable'::regclass,
'attname', 'col1'::name,
'inherited', false,
'avg_width', 125::integer,
'null_frac', 0.5::real);
</programlisting>
</para>
<para>
Minor errors are reported as a <literal>WARNING</literal> and
ignored, and remaining statistics will still be restored. If all
specified statistics are successfully restored, return
<literal>true</literal>, otherwise <literal>false</literal>.
</para>
</entry>
</row>
</tbody>
</tgroup>
</table>

<para>
<xref linkend="functions-info-partition"/> lists functions that provide
information about the structure of partitioned tables.
Expand Down
32 changes: 32 additions & 0 deletions src/backend/catalog/system_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,38 @@ LANGUAGE internal
STRICT IMMUTABLE PARALLEL SAFE
AS 'unicode_is_normalized';

CREATE OR REPLACE FUNCTION
pg_set_relation_stats(relation regclass,
relpages integer DEFAULT NULL,
reltuples real DEFAULT NULL,
relallvisible integer DEFAULT NULL)
RETURNS void
LANGUAGE INTERNAL
CALLED ON NULL INPUT VOLATILE
AS 'pg_set_relation_stats';

CREATE OR REPLACE FUNCTION
pg_set_attribute_stats(relation regclass,
attname name,
inherited bool,
null_frac real DEFAULT NULL,
avg_width integer DEFAULT NULL,
n_distinct real DEFAULT NULL,
most_common_vals text DEFAULT NULL,
most_common_freqs real[] DEFAULT NULL,
histogram_bounds text DEFAULT NULL,
correlation real DEFAULT NULL,
most_common_elems text DEFAULT NULL,
most_common_elem_freqs real[] DEFAULT NULL,
elem_count_histogram real[] DEFAULT NULL,
range_length_histogram text DEFAULT NULL,
range_empty_frac real DEFAULT NULL,
range_bounds_histogram text DEFAULT NULL)
RETURNS void
LANGUAGE INTERNAL
CALLED ON NULL INPUT VOLATILE
AS 'pg_set_attribute_stats';

--
-- The default permissions for functions mean that anyone can execute them.
-- A number of functions shouldn't be executable by just anyone, but rather
Expand Down
5 changes: 4 additions & 1 deletion src/backend/statistics/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ top_builddir = ../../..
include $(top_builddir)/src/Makefile.global

OBJS = \
attribute_stats.o \
dependencies.o \
extended_stats.o \
mcv.o \
mvdistinct.o
mvdistinct.o \
relation_stats.o \
stat_utils.o

include $(top_srcdir)/src/backend/common.mk
Loading
Loading