|
| 1 | +<!-- doc/src/sgml/dump_stat.sgml --> |
| 2 | + |
| 3 | +<sect1 id="dump-stat" xreflabel="dump-stat"> |
| 4 | + <title>dump_stat</title> |
| 5 | + |
| 6 | + <indexterm zone="dump-stat"> |
| 7 | + <primary>dump_stat</primary> |
| 8 | + </indexterm> |
| 9 | + |
| 10 | + <para> |
| 11 | + The <filename>dump_stat</> module provides functions that allow you to |
| 12 | + backup and recover the contents of the |
| 13 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 14 | + table. The <function>dump_statistic</> function generates <literal>INSERT</> |
| 15 | + statements which can later be applied to a compatible database. The extension |
| 16 | + should be installed on the recipient server in order to successfuly restore |
| 17 | + statistical data since these statements heavily rely on the provided functions. |
| 18 | + </para> |
| 19 | + |
| 20 | + <para> |
| 21 | + Note that the definition of the |
| 22 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 23 | + table might change occasionally, which means that generated dump might be incompatible |
| 24 | + with future releases of <productname>PostgreSQL</productname>. |
| 25 | + </para> |
| 26 | + |
| 27 | + <sect2> |
| 28 | + <title>Functions</title> |
| 29 | + |
| 30 | + <variablelist> |
| 31 | + <varlistentry> |
| 32 | + <term> |
| 33 | + <function>dump_statistic() returns setof text</function> |
| 34 | + <indexterm> |
| 35 | + <primary>dump_statistic</primary> |
| 36 | + </indexterm> |
| 37 | + </term> |
| 38 | + |
| 39 | + <listitem> |
| 40 | + <para> |
| 41 | + <function>dump_statistic</function> dumps the contents of the |
| 42 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 43 | + system catalog. It produces an <literal>INSERT</> statement |
| 44 | + per each tuple of the |
| 45 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link>, |
| 46 | + excluding the ones that contain statistical data for tables in the |
| 47 | + <literal>information_schema</> and <literal>pg_catalog</> schemas. |
| 48 | + </para> |
| 49 | + |
| 50 | + <para> |
| 51 | + The <literal>INSERT</> statement takes form of |
| 52 | +<screen> |
| 53 | +WITH upsert as ( |
| 54 | + UPDATE pg_catalog.pg_statistic SET <replaceable class="PARAMETER">column_name</> = <replaceable class="PARAMETER">expression</> [, ...] |
| 55 | + WHERE to_schema_qualified_relation(starelid) = <replaceable class="PARAMETER">t_relname</> |
| 56 | + AND to_attname(<replaceable class="PARAMETER">t_relname</>, staattnum) = <replaceable class="PARAMETER">t_attname</> |
| 57 | + AND to_atttype(<replaceable class="PARAMETER">t_relname</>, staattnum) = <replaceable class="PARAMETER">t_atttype</> |
| 58 | + AND stainherit = <replaceable class="PARAMETER">t_stainherit</> |
| 59 | + RETURNING *) |
| 60 | +ins as ( |
| 61 | + SELECT <replaceable class="PARAMETER">expression</> [, ...] |
| 62 | + WHERE NOT EXISTS (SELECT * FROM upsert) |
| 63 | + AND to_attnum(<replaceable class="PARAMETER">t_relname</>, <replaceable class="PARAMETER">t_attname</>) IS NOT NULL |
| 64 | + AND to_atttype(<replaceable class="PARAMETER">t_relname</>, <replaceable class="PARAMETER">t_attname</>) = <replaceable class="PARAMETER">t_atttype</>) |
| 65 | +INSERT INTO pg_catalog.pg_statistic SELECT * FROM ins; |
| 66 | + |
| 67 | +where <replaceable class="PARAMETER">expression</> can be one of: |
| 68 | + |
| 69 | +array_in(<replaceable class="PARAMETER">array_text</>, <replaceable class="PARAMETER">type_name</>::regtype::oid, -1) |
| 70 | +<replaceable class="PARAMETER">value</>::<replaceable class="PARAMETER">type_name</> |
| 71 | +</screen> |
| 72 | + </para> |
| 73 | + |
| 74 | + <para> |
| 75 | + Produced statements could be saved to a file, e.g. |
| 76 | +<screen> |
| 77 | +$ psql test -A |
| 78 | +test=# \t |
| 79 | +test=# \o dump_stat.sql |
| 80 | +test=# select dump_statistic(); |
| 81 | +</screen> |
| 82 | + </para> |
| 83 | + </listitem> |
| 84 | + </varlistentry> |
| 85 | + |
| 86 | + <varlistentry> |
| 87 | + <term> |
| 88 | + <function>dump_statistic(schema_name text) returns setof text</function> |
| 89 | + </term> |
| 90 | + |
| 91 | + <listitem> |
| 92 | + <para> |
| 93 | + <function>dump_statistic</function> dumps the contents of the |
| 94 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 95 | + system catalog. It produces an <literal>INSERT</> statement |
| 96 | + per each tuple of the |
| 97 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 98 | + that relates to some table in the <literal>schema_name</> schema. |
| 99 | + </para> |
| 100 | + </listitem> |
| 101 | + </varlistentry> |
| 102 | + |
| 103 | + <varlistentry> |
| 104 | + <term> |
| 105 | + <function>dump_statistic(schema_name text, table_name text) returns setof text</function> |
| 106 | + </term> |
| 107 | + |
| 108 | + <listitem> |
| 109 | + <para> |
| 110 | + <function>dump_statistic</function> dumps the contents of the |
| 111 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 112 | + system catalog. It produces an <literal>INSERT</> statement |
| 113 | + per each tuple of the |
| 114 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 115 | + that relates to the specified <literal>schema_name.table_name</> table. |
| 116 | + </para> |
| 117 | + </listitem> |
| 118 | + </varlistentry> |
| 119 | + |
| 120 | + <varlistentry> |
| 121 | + <term> |
| 122 | + <function>dump_statistic(relid oid) returns setof text</function> |
| 123 | + </term> |
| 124 | + |
| 125 | + <listitem> |
| 126 | + <para> |
| 127 | + <function>dump_statistic</function> dumps the contents of the |
| 128 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 129 | + system catalog. It produces an <literal>INSERT</> statement |
| 130 | + per each tuple of the |
| 131 | + <link linkend="catalog-pg-statistic"><structname>pg_statistic</structname></link> |
| 132 | + that contains statistical data for the relation referenced by <literal>relid</>. |
| 133 | + </para> |
| 134 | + </listitem> |
| 135 | + </varlistentry> |
| 136 | + |
| 137 | + <varlistentry> |
| 138 | + <term> |
| 139 | + <function>to_schema_qualified_operator(opid oid) returns text</function> |
| 140 | + <indexterm> |
| 141 | + <primary>to_schema_qualified_operator</primary> |
| 142 | + </indexterm> |
| 143 | + </term> |
| 144 | + |
| 145 | + <listitem> |
| 146 | + <para> |
| 147 | + Fetches the schema-qualified operator name by operator id <literal>opid</>. For example: |
| 148 | + </para> |
| 149 | +<screen> |
| 150 | +test=# SELECT to_schema_qualified_operator('+(int,int)'::regoperator); |
| 151 | + to_schema_qualified_operator |
| 152 | +------------------------------------------------ |
| 153 | + pg_catalog.+(pg_catalog.int4, pg_catalog.int4) |
| 154 | +(1 row) |
| 155 | +</screen> |
| 156 | + </listitem> |
| 157 | + </varlistentry> |
| 158 | + |
| 159 | + <varlistentry> |
| 160 | + <term> |
| 161 | + <function>to_schema_qualified_type(typid oid) returns text</function> |
| 162 | + <indexterm> |
| 163 | + <primary>to_schema_qualified_type</primary> |
| 164 | + </indexterm> |
| 165 | + </term> |
| 166 | + |
| 167 | + <listitem> |
| 168 | + <para> |
| 169 | + Fetches the schema-qualified type name by type id <literal>typid</>. |
| 170 | + </para> |
| 171 | + </listitem> |
| 172 | + </varlistentry> |
| 173 | + |
| 174 | + <varlistentry> |
| 175 | + <term> |
| 176 | + <function>to_schema_qualified_relation(relid oid) returns text</function> |
| 177 | + <indexterm> |
| 178 | + <primary>to_schema_qualified_relation</primary> |
| 179 | + </indexterm> |
| 180 | + </term> |
| 181 | + |
| 182 | + <listitem> |
| 183 | + <para> |
| 184 | + Fetches the schema-qualified relation name by relation id <literal>relid</>. |
| 185 | + </para> |
| 186 | + </listitem> |
| 187 | + </varlistentry> |
| 188 | + |
| 189 | + <varlistentry> |
| 190 | + <term> |
| 191 | + <function>anyarray_elemtype(arr anyarray) returns oid</function> |
| 192 | + <indexterm> |
| 193 | + <primary>anyarray_elemtype</primary> |
| 194 | + </indexterm> |
| 195 | + </term> |
| 196 | + |
| 197 | + <listitem> |
| 198 | + <para> |
| 199 | + Returns the element type of the given array as <literal>oid</>. For example: |
| 200 | + </para> |
| 201 | +<screen> |
| 202 | +test=# SELECT anyarray_elemtype(array_in('{1,2,3}', 'int'::regtype, -1)); |
| 203 | + anyarray_elemtype |
| 204 | +------------------- |
| 205 | + 23 |
| 206 | +(1 row) |
| 207 | +</screen> |
| 208 | + </listitem> |
| 209 | + </varlistentry> |
| 210 | + |
| 211 | + <varlistentry> |
| 212 | + <term> |
| 213 | + <function>to_attname(relation text, colnum int2) returns text</function> |
| 214 | + <indexterm> |
| 215 | + <primary>to_attname</primary> |
| 216 | + </indexterm> |
| 217 | + </term> |
| 218 | + |
| 219 | + <listitem> |
| 220 | + <para> |
| 221 | + Given a relation name <literal>relation</> and a column number |
| 222 | + <literal>colnum</>, returns the column name as <literal>text</>. |
| 223 | + </para> |
| 224 | + </listitem> |
| 225 | + </varlistentry> |
| 226 | + |
| 227 | + <varlistentry> |
| 228 | + <term> |
| 229 | + <function>to_attnum(relation text, col text) returns int2</function> |
| 230 | + <indexterm> |
| 231 | + <primary>to_attnum</primary> |
| 232 | + </indexterm> |
| 233 | + </term> |
| 234 | + |
| 235 | + <listitem> |
| 236 | + <para> |
| 237 | + Given a relation name <literal>relation</> and a column name |
| 238 | + <literal>col</>, returns the column number as <literal>int2</>. |
| 239 | + </para> |
| 240 | + </listitem> |
| 241 | + </varlistentry> |
| 242 | + |
| 243 | + <varlistentry> |
| 244 | + <term> |
| 245 | + <function>to_atttype(relation text, col text) returns text</function> |
| 246 | + <indexterm> |
| 247 | + <primary>to_atttype</primary> |
| 248 | + </indexterm> |
| 249 | + </term> |
| 250 | + |
| 251 | + <listitem> |
| 252 | + <para> |
| 253 | + Given a relation name <literal>relation</> and a column name |
| 254 | + <literal>col</>, returns the schema-qualified column type as <literal>text</>. |
| 255 | + </para> |
| 256 | + </listitem> |
| 257 | + </varlistentry> |
| 258 | + |
| 259 | + <varlistentry> |
| 260 | + <term> |
| 261 | + <function>to_atttype(relation text, colnum int2) returns text</function> |
| 262 | + </term> |
| 263 | + |
| 264 | + <listitem> |
| 265 | + <para> |
| 266 | + Given a relation name <literal>relation</> and a column number |
| 267 | + <literal>colnum</>, returns the schema-qualified column type as <literal>text</>. |
| 268 | + </para> |
| 269 | + </listitem> |
| 270 | + </varlistentry> |
| 271 | + |
| 272 | + <varlistentry> |
| 273 | + <term> |
| 274 | + <function>to_namespace(nsp text) returns oid</function> |
| 275 | + <indexterm> |
| 276 | + <primary>to_namespace</primary> |
| 277 | + </indexterm> |
| 278 | + </term> |
| 279 | + |
| 280 | + <listitem> |
| 281 | + <para> |
| 282 | + <function>to_namespace</function> duplicates the behavior of |
| 283 | + the cast to the |
| 284 | + <link linkend="datatype-oid"><structname>regnamespace</structname></link> |
| 285 | + type, which is not present in the |
| 286 | + <productname>PostgreSQL</productname> 9.4 release (and prior releases). |
| 287 | + This function returns the <literal>oid</> of the given schema. |
| 288 | + </para> |
| 289 | + </listitem> |
| 290 | + </varlistentry> |
| 291 | + |
| 292 | + <varlistentry> |
| 293 | + <term> |
| 294 | + <function>get_namespace(relation oid) returns oid</function> |
| 295 | + <indexterm> |
| 296 | + <primary>get_namespace</primary> |
| 297 | + </indexterm> |
| 298 | + </term> |
| 299 | + |
| 300 | + <listitem> |
| 301 | + <para> |
| 302 | + <function>get_namespace</function> returns the schema |
| 303 | + of the given relation as <literal>oid</>. |
| 304 | + </para> |
| 305 | + </listitem> |
| 306 | + </varlistentry> |
| 307 | + </variablelist> |
| 308 | + </sect2> |
| 309 | +</sect1> |