# 51.50. pg\_statistic

系統目錄 pg\_statistic 儲存有關資料庫內容的統計數據。內容項目是由 [ANALYZE](/12/reference/sql-commands/analyze.md) 所建立的，隨後由查詢計劃程序使用。請注意，所有統計數據本質上都是趨近的，即使假設上它是最新的數據。

Normally there is one entry, with `stainherit` = `false`, for each table column that has been analyzed. If the table has inheritance children, a second entry with `stainherit` = `true` is also created. This row represents the column's statistics over the inheritance tree, i.e., statistics for the data you'd see with `SELECT` *`column`* FROM *`table`*\*, whereas the `stainherit` = `false` row represents the results of `SELECT` *`column`* FROM ONLY *`table`*.

`pg_statistic` also stores statistical data about the values of index expressions. These are described as if they were actual data columns; in particular, `starelid` references the index. No entry is made for an ordinary non-expression index column, however, since it would be redundant with the entry for the underlying table column. Currently, entries for index expressions always have `stainherit` = `false`.

Since different kinds of statistics might be appropriate for different kinds of data, `pg_statistic` is designed not to assume very much about what sort of statistics it stores. Only extremely general statistics (such as nullness) are given dedicated columns in `pg_statistic`. Everything else is stored in “slots”, which are groups of associated columns whose content is identified by a code number in one of the slot's columns. For more information see `src/include/catalog/pg_statistic.h`.

`pg_statistic` should not be readable by the public, since even statistical information about a table's contents might be considered sensitive. (Example: minimum and maximum values of a salary column might be quite interesting.) [`pg_stats`](https://www.postgresql.org/docs/10/static/view-pg-stats.html) is a publicly readable view on `pg_statistic` that only exposes information about those tables that are readable by the current user.

## **Table 51.50. `pg_statistic` Columns**

| Name              | Type       | References                                                                                   | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| ----------------- | ---------- | -------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `starelid`        | `oid`      | [`pg_class`](https://www.postgresql.org/docs/10/static/catalog-pg-class.html).oid            | The table or index that the described column belongs to                                                                                                                                                                                                                                                                                                                                                                                                      |
| `staattnum`       | `int2`     | [`pg_attribute`](https://www.postgresql.org/docs/10/static/catalog-pg-attribute.html).attnum | The number of the described column                                                                                                                                                                                                                                                                                                                                                                                                                           |
| `stainherit`      | `bool`     |                                                                                              | If true, the stats include inheritance child columns, not just the values in the specified relation                                                                                                                                                                                                                                                                                                                                                          |
| `stanullfrac`     | `float4`   |                                                                                              | The fraction of the column's entries that are null                                                                                                                                                                                                                                                                                                                                                                                                           |
| `stawidth`        | `int4`     |                                                                                              | The average stored width, in bytes, of nonnull entries                                                                                                                                                                                                                                                                                                                                                                                                       |
| `stadistinct`     | `float4`   |                                                                                              | The number of distinct nonnull data values in the column. A value greater than zero is the actual number of distinct values. A value less than zero is the negative of a multiplier for the number of rows in the table; for example, a column in which about 80% of the values are nonnull and each nonnull value appears about twice on average could be represented by `stadistinct` = -0.4. A zero value means the number of distinct values is unknown. |
| `stakind`*`N`*    | `int2`     |                                                                                              | A code number indicating the kind of statistics stored in the \_`N`\_th “slot” of the `pg_statistic` row.                                                                                                                                                                                                                                                                                                                                                    |
| `staop`*`N`*      | `oid`      | [`pg_operator`](https://www.postgresql.org/docs/10/static/catalog-pg-operator.html).oid      | An operator used to derive the statistics stored in the \_`N`\_th “slot”. For example, a histogram slot would show the `<` operator that defines the sort order of the data.                                                                                                                                                                                                                                                                                 |
| `stanumbers`*`N`* | `float4[]` |                                                                                              | Numerical statistics of the appropriate kind for the \_`N`\_th “slot”, or null if the slot kind does not involve numerical values                                                                                                                                                                                                                                                                                                                            |
| `stavalues`*`N`*  | `anyarray` |                                                                                              | Column data values of the appropriate kind for the \_`N`\_th “slot”, or null if the slot kind does not store any data values. Each array's element values are actually of the specific column's data type, or a related type such as an array's element type, so there is no way to define these columns' type more specifically than `anyarray`.                                                                                                            |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.postgresql.tw/12/internals/system-catalogs/pg_statistic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
