OSDN Git Service

Make environment for 1.3.5
authorKyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Wed, 12 Nov 2014 07:51:32 +0000 (16:51 +0900)
committerKyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Fri, 14 Nov 2014 00:56:14 +0000 (09:56 +0900)
23 files changed:
Makefile
SPECS/pg_dbms_stats91.spec
SPECS/pg_dbms_stats92.spec
SPECS/pg_dbms_stats93.spec
SPECS/pg_dbms_stats94.spec
expected/init-9.4.out [new file with mode: 0644]
expected/ut-9.4.out [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.0--1.3.2-9.4.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.2--1.3.3-9.4.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.3--1.3.4-9.4.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.1.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.2.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.3.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.4.sql [new file with mode: 0644]
ext_scripts/pg_dbms_stats--1.3.5-9.1.sql [moved from ext_scripts/pg_dbms_stats--1.3.4-9.1.sql with 99% similarity]
ext_scripts/pg_dbms_stats--1.3.5-9.2.sql [moved from ext_scripts/pg_dbms_stats--1.3.4-9.2.sql with 99% similarity]
ext_scripts/pg_dbms_stats--1.3.5-9.3.sql [moved from ext_scripts/pg_dbms_stats--1.3.4-9.3.sql with 99% similarity]
ext_scripts/pg_dbms_stats--1.3.5-9.4.sql [new file with mode: 0644]
input/ut_imp_exp-9.4.source [new file with mode: 0644]
output/ut_imp_exp-9.4.source [new file with mode: 0644]
pg_dbms_stats.control
sql/init-9.4.sql [new file with mode: 0644]
sql/ut-9.4.sql [new file with mode: 0644]

index 59a84f6..2048d83 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 
 MODULE_big = pg_dbms_stats
 OBJS = pg_dbms_stats.o dump.o import.o
-DBMSSTATSVER = 1.3.4
+DBMSSTATSVER = 1.3.5
 DOCDIR = doc
 EXTDIR = ext_scripts
 
@@ -14,13 +14,14 @@ LAST_LIBPATH=$(shell echo $(LD_LIBRARY_PATH) | sed -e "s/^.*;//")
 CHECKING=$(shell echo $(LAST_LIBPATH)| grep './tmp_check/install/' | wc -l)
 
 EXTENSION = pg_dbms_stats
-DATA = pg_dbms_stats--1.3.4.sql pg_dbms_stats--1.0--1.3.2.sql pg_dbms_stats--1.3.2--1.3.3.sql pg_dbms_stats--1.3.3--1.3.4.sql
 
 REGRESS = init-common ut_fdw_init init-$(MAJORVERSION) ut-common \
                  ut-$(MAJORVERSION)  ut_imp_exp-$(MAJORVERSION)
 
 REGRESS_OPTS = --encoding=UTF8 --temp-config=regress.conf --extra-install=contrib/file_fdw
 
+DATA = pg_dbms_stats--1.3.5.sql pg_dbms_stats--1.0--1.3.2.sql pg_dbms_stats--1.3.2--1.3.3.sql pg_dbms_stats--1.3.3--1.3.4.sql pg_dbms_stats--1.3.4--1.3.5.sql
+
 DOCS = $(DOCDIR)/export_effective_stats-$(MAJORVERSION).sql.sample \
        $(DOCDIR)/export_plain_stats-$(MAJORVERSION).sql.sample
 
@@ -54,11 +55,8 @@ include $(top_builddir)/src/Makefile.global
 include $(top_srcdir)/contrib/contrib-global.mk
 endif
 
-ifeq "$(MAJORVERSION)" "9.4"
-MAJORVERSION=9.3
-endif
 ifeq "$(MAJORVERSION)" "9.5"
-MAJORVERSION=9.3
+MAJORVERSION=9.4
 endif
 
 TARSOURCES = Makefile *.c  *.h \
index 5023687..6e20829 100644 (file)
@@ -14,8 +14,8 @@
 ## Set general information for pg_dbms_stats.
 Summary:    Plan Stabilizer for PostgreSQL 9.1
 Name:       pg_dbms_stats91
-Version:    1.3.4
-Release:    2%{?dist}
+Version:    1.3.5
+Release:    1%{?dist}
 License:    BSD
 Group:      Applications/Databases
 Source:     %{name}-%{version}.tar.gz
@@ -56,10 +56,11 @@ rm -rf %{buildroot}
 install -d %{buildroot}%{_libdir}
 install -m 755 pg_dbms_stats.so %{buildroot}%{_libdir}/pg_dbms_stats.so
 install -d %{buildroot}%{_datadir}/extension
-install -m 644 pg_dbms_stats--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 install -m 644 pg_dbms_stats--1.0--1.3.2.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 install -m 644 pg_dbms_stats--1.3.2--1.3.3.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 install -m 644 pg_dbms_stats--1.3.3--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.4--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 install -m 644 pg_dbms_stats.control %{buildroot}%{_datadir}/extension/pg_dbms_stats.control
 install -d %{buildroot}%{_docdir}/extension
 install -m 644 doc/export_effective_stats-9.1.sql.sample %{buildroot}%{_docdir}/extension/export_effective_stats-9.1.sql.sample
@@ -72,10 +73,11 @@ rm -rf %{buildroot}
 %defattr(0755,root,root)
 %{_libdir}/pg_dbms_stats.so
 %defattr(0644,root,root)
-%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats.control
 %{_docdir}/extension/export_effective_stats-9.1.sql.sample
 %{_docdir}/extension/export_plain_stats-9.1.sql.sample
index 358084f..e4b77c9 100644 (file)
@@ -14,8 +14,8 @@
 ## Set general information for pg_dbms_stats.
 Summary:    Plan Stabilizer for PostgreSQL 9.2
 Name:       pg_dbms_stats92
-Version:    1.3.4
-Release:    2%{?dist}
+Version:    1.3.5
+Release:    1%{?dist}
 License:    BSD
 Group:      Applications/Databases
 Source:     %{name}-%{version}.tar.gz
@@ -56,10 +56,11 @@ rm -rf %{buildroot}
 install -d %{buildroot}%{_libdir}
 install -m 755 pg_dbms_stats.so %{buildroot}%{_libdir}/pg_dbms_stats.so
 install -d %{buildroot}%{_datadir}/extension
-install -m 644 pg_dbms_stats--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 install -m 644 pg_dbms_stats--1.0--1.3.2.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 install -m 644 pg_dbms_stats--1.3.2--1.3.3.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 install -m 644 pg_dbms_stats--1.3.3--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.4--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 install -m 644 pg_dbms_stats.control %{buildroot}%{_datadir}/extension/pg_dbms_stats.control
 install -d %{buildroot}%{_docdir}/extension
 install -m 644 doc/export_effective_stats-9.2.sql.sample %{buildroot}%{_docdir}/extension/export_effective_stats-9.2.sql.sample
@@ -72,10 +73,11 @@ rm -rf %{buildroot}
 %defattr(0755,root,root)
 %{_libdir}/pg_dbms_stats.so
 %defattr(0644,root,root)
-%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats.control
 %{_docdir}/extension/export_effective_stats-9.2.sql.sample
 %{_docdir}/extension/export_plain_stats-9.2.sql.sample
index 039b111..c4b77c3 100644 (file)
@@ -14,8 +14,8 @@
 ## Set general information for pg_dbms_stats.
 Summary:    Plan Stabilizer for PostgreSQL 9.3
 Name:       pg_dbms_stats93
-Version:    1.3.4
-Release:    2%{?dist}
+Version:    1.3.5
+Release:    1%{?dist}
 License:    BSD
 Group:      Applications/Databases
 Source:     %{name}-%{version}.tar.gz
@@ -61,10 +61,11 @@ rm -rf %{buildroot}
 install -d %{buildroot}%{_libdir}
 install -m 755 pg_dbms_stats.so %{buildroot}%{_libdir}/pg_dbms_stats.so
 install -d %{buildroot}%{_datadir}/extension
-install -m 644 pg_dbms_stats--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 install -m 644 pg_dbms_stats--1.0--1.3.2.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 install -m 644 pg_dbms_stats--1.3.2--1.3.3.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 install -m 644 pg_dbms_stats--1.3.3--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.4--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 install -m 644 pg_dbms_stats.control %{buildroot}%{_datadir}/extension/pg_dbms_stats.control
 install -d %{buildroot}%{_docdir}/extension
 install -m 644 doc/export_effective_stats-9.3.sql.sample %{buildroot}%{_docdir}/extension/export_effective_stats-9.3.sql.sample
@@ -77,10 +78,11 @@ rm -rf %{buildroot}
 %defattr(0755,root,root)
 %{_libdir}/pg_dbms_stats.so
 %defattr(0644,root,root)
-%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats--1.0--1.3.2.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.2--1.3.3.sql
 %{_datadir}/extension/pg_dbms_stats--1.3.3--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.4--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats.control
 %{_docdir}/extension/export_effective_stats-9.3.sql.sample
 %{_docdir}/extension/export_plain_stats-9.3.sql.sample
index 2f3f4ef..cf6e449 100644 (file)
@@ -14,8 +14,8 @@
 ## Set general information for pg_dbms_stats.
 Summary:    Plan Stabilizer for PostgreSQL 9.4
 Name:       pg_dbms_stats94
-Version:    1.3.4
-Release:    2%{?dist}
+Version:    1.3.5
+Release:    1%{?dist}
 License:    BSD
 Group:      Applications/Databases
 Source:     %{name}-%{version}.tar.gz
@@ -61,11 +61,11 @@ rm -rf %{buildroot}
 install -d %{buildroot}%{_libdir}
 install -m 755 pg_dbms_stats.so %{buildroot}%{_libdir}/pg_dbms_stats.so
 install -d %{buildroot}%{_datadir}/extension
-install -m 644 pg_dbms_stats--1.3.4.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+install -m 644 pg_dbms_stats--1.3.5.sql %{buildroot}%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 install -m 644 pg_dbms_stats.control %{buildroot}%{_datadir}/extension/pg_dbms_stats.control
 install -d %{buildroot}%{_docdir}/extension
-install -m 644 doc/export_effective_stats-9.3.sql.sample %{buildroot}%{_docdir}/extension/export_effective_stats-9.4.sql.sample
-install -m 644 doc/export_plain_stats-9.3.sql.sample %{buildroot}%{_docdir}/extension/export_plain_stats-9.4.sql.sample
+install -m 644 doc/export_effective_stats-9.4.sql.sample %{buildroot}%{_docdir}/extension/export_effective_stats-9.4.sql.sample
+install -m 644 doc/export_plain_stats-9.4.sql.sample %{buildroot}%{_docdir}/extension/export_plain_stats-9.4.sql.sample
 
 %clean
 rm -rf %{buildroot}
@@ -74,7 +74,7 @@ rm -rf %{buildroot}
 %defattr(0755,root,root)
 %{_libdir}/pg_dbms_stats.so
 %defattr(0644,root,root)
-%{_datadir}/extension/pg_dbms_stats--1.3.4.sql
+%{_datadir}/extension/pg_dbms_stats--1.3.5.sql
 %{_datadir}/extension/pg_dbms_stats.control
 %{_docdir}/extension/export_effective_stats-9.4.sql.sample
 %{_docdir}/extension/export_plain_stats-9.4.sql.sample
diff --git a/expected/init-9.4.out b/expected/init-9.4.out
new file mode 100644 (file)
index 0000000..6a5b537
--- /dev/null
@@ -0,0 +1,113 @@
+CREATE MATERIALIZED VIEW s0.smv0 AS
+SELECT st0.id,
+       st0.num,
+       st2.txt
+  FROM s0.st0,s0.st2
+ WHERE st0.id = st2.id
+ ORDER BY id;
+CREATE VIEW plain_relations_statistic_v AS
+SELECT oid::regclass,
+       relpages,
+       reltuples,
+       relallvisible,
+       pg_relation_size(oid) / 8192 curpages
+  FROM pg_class
+ ORDER BY oid::regclass::text;
+CREATE VIEW relations_locked_v AS
+SELECT relid::regclass,
+       relname,
+       relpages,
+       reltuples,
+       relallvisible,
+       curpages
+  FROM dbms_stats._relation_stats_locked
+ ORDER BY relid;
+CREATE VIEW relations_backup_v AS
+SELECT id,
+       relid::regclass,
+       relname,
+       relpages,
+       reltuples,
+       relallvisible,
+       curpages
+  FROM dbms_stats.relation_stats_backup
+ ORDER BY id, relid;
+CREATE VIEW plain_columns_statistic_v AS
+SELECT starelid::regclass, staattnum, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1::text, stavalues2::text, stavalues3::text, stavalues4::text, stavalues5::text
+  FROM pg_statistic
+ ORDER BY starelid, staattnum, stainherit;
+CREATE VIEW columns_locked_v AS
+SELECT starelid::regclass, staattnum, attname, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats._column_stats_locked c
+  JOIN pg_attribute a
+    ON (c.starelid = a.attrelid AND c.staattnum = a.attnum)
+ ORDER BY starelid, staattnum, stainherit;
+CREATE VIEW columns_backup_v AS
+SELECT id, statypid,
+       starelid::regclass, staattnum, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats.column_stats_backup
+ ORDER BY id, starelid, staattnum, stainherit;
+CREATE TABLE dbms_stats.work (
+  nspname          name   NOT NULL,
+  relname          name   NOT NULL,
+  relpages         int4   NOT NULL,
+  reltuples        float4 NOT NULL,
+  relallvisible    int4   NOT NULL,
+  curpages         int4   NOT NULL,
+  last_analyze     timestamp with time zone,
+  last_autoanalyze timestamp with time zone,
+  attname          name,
+  nspname_of_typename name,
+  typname          name,
+  atttypmod        int4,
+  stainherit       bool,
+  stanullfrac      float4,
+  stawidth         int4,
+  stadistinct      float4,
+  stakind1         int2,
+  stakind2         int2,
+  stakind3         int2,
+  stakind4         int2,
+  stakind5         int2,
+  staop1           oid,
+  staop2           oid,
+  staop3           oid,
+  staop4           oid,
+  staop5           oid,
+  stanumbers1      float4[],
+  stanumbers2      float4[],
+  stanumbers3      float4[],
+  stanumbers4      float4[],
+  stanumbers5      float4[],
+  stavalues1       dbms_stats.anyarray,
+  stavalues2       dbms_stats.anyarray,
+  stavalues3       dbms_stats.anyarray,
+  stavalues4       dbms_stats.anyarray
+ ,stavalues5       dbms_stats.anyarray
+);
+CREATE VIEW work_v AS
+SELECT nspname, relname, relpages, reltuples, relallvisible,
+       curpages, attname, nspname_of_typename, typname, atttypmod,
+       stainherit, stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats.work
+ ORDER BY nspname, relname, attname, stainherit;
+ANALYZE s0.sft0;
diff --git a/expected/ut-9.4.out b/expected/ut-9.4.out
new file mode 100644 (file)
index 0000000..f7a3313
--- /dev/null
@@ -0,0 +1,10140 @@
+\pset null '(null)'
+/*
+ * No.2-1 table definitions.
+ */
+-- No.2-1-1
+\d dbms_stats.backup_history
+                                      Table "dbms_stats.backup_history"
+ Column  |           Type           |                               Modifiers                                
+---------+--------------------------+------------------------------------------------------------------------
+ id      | bigint                   | not null default nextval('dbms_stats.backup_history_id_seq'::regclass)
+ time    | timestamp with time zone | not null
+ unit    | character(1)             | not null
+ comment | text                     | 
+Indexes:
+    "backup_history_pkey" PRIMARY KEY, btree (id)
+Referenced by:
+    TABLE "dbms_stats.column_stats_backup" CONSTRAINT "column_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
+    TABLE "dbms_stats.relation_stats_backup" CONSTRAINT "relation_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
+
+-- No.2-1-2
+\d dbms_stats.column_stats_backup
+    Table "dbms_stats.column_stats_backup"
+   Column    |        Type         | Modifiers 
+-------------+---------------------+-----------
+ id          | bigint              | not null
+ statypid    | oid                 | not null
+ starelid    | oid                 | not null
+ staattnum   | smallint            | not null
+ stainherit  | boolean             | not null
+ stanullfrac | real                | not null
+ stawidth    | integer             | not null
+ stadistinct | real                | not null
+ stakind1    | smallint            | not null
+ stakind2    | smallint            | not null
+ stakind3    | smallint            | not null
+ stakind4    | smallint            | not null
+ stakind5    | smallint            | not null
+ staop1      | oid                 | not null
+ staop2      | oid                 | not null
+ staop3      | oid                 | not null
+ staop4      | oid                 | not null
+ staop5      | oid                 | not null
+ stanumbers1 | real[]              | 
+ stanumbers2 | real[]              | 
+ stanumbers3 | real[]              | 
+ stanumbers4 | real[]              | 
+ stanumbers5 | real[]              | 
+ stavalues1  | dbms_stats.anyarray | 
+ stavalues2  | dbms_stats.anyarray | 
+ stavalues3  | dbms_stats.anyarray | 
+ stavalues4  | dbms_stats.anyarray | 
+ stavalues5  | dbms_stats.anyarray | 
+Indexes:
+    "column_stats_backup_pkey" PRIMARY KEY, btree (id, starelid, staattnum, stainherit)
+Foreign-key constraints:
+    "column_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
+    "column_stats_backup_id_fkey1" FOREIGN KEY (id, starelid) REFERENCES dbms_stats.relation_stats_backup(id, relid) ON DELETE CASCADE
+
+-- No.2-1-3
+\d dbms_stats._column_stats_locked
+    Table "dbms_stats._column_stats_locked"
+   Column    |        Type         | Modifiers 
+-------------+---------------------+-----------
+ starelid    | oid                 | not null
+ staattnum   | smallint            | not null
+ stainherit  | boolean             | not null
+ stanullfrac | real                | 
+ stawidth    | integer             | 
+ stadistinct | real                | 
+ stakind1    | smallint            | 
+ stakind2    | smallint            | 
+ stakind3    | smallint            | 
+ stakind4    | smallint            | 
+ stakind5    | smallint            | 
+ staop1      | oid                 | 
+ staop2      | oid                 | 
+ staop3      | oid                 | 
+ staop4      | oid                 | 
+ staop5      | oid                 | 
+ stanumbers1 | real[]              | 
+ stanumbers2 | real[]              | 
+ stanumbers3 | real[]              | 
+ stanumbers4 | real[]              | 
+ stanumbers5 | real[]              | 
+ stavalues1  | dbms_stats.anyarray | 
+ stavalues2  | dbms_stats.anyarray | 
+ stavalues3  | dbms_stats.anyarray | 
+ stavalues4  | dbms_stats.anyarray | 
+ stavalues5  | dbms_stats.anyarray | 
+Indexes:
+    "_column_stats_locked_pkey" PRIMARY KEY, btree (starelid, staattnum, stainherit)
+Foreign-key constraints:
+    "_column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats._relation_stats_locked(relid) ON DELETE CASCADE
+Triggers:
+    invalidate_column_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats._column_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_column_cache()
+
+-- No.2-1-4
+\d dbms_stats.relation_stats_backup
+        Table "dbms_stats.relation_stats_backup"
+      Column      |           Type           | Modifiers 
+------------------+--------------------------+-----------
+ id               | bigint                   | not null
+ relid            | oid                      | not null
+ relname          | text                     | not null
+ relpages         | integer                  | not null
+ reltuples        | real                     | not null
+ relallvisible    | integer                  | not null
+ curpages         | integer                  | not null
+ last_analyze     | timestamp with time zone | 
+ last_autoanalyze | timestamp with time zone | 
+Indexes:
+    "relation_stats_backup_pkey" PRIMARY KEY, btree (id, relid)
+Foreign-key constraints:
+    "relation_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
+Referenced by:
+    TABLE "dbms_stats.column_stats_backup" CONSTRAINT "column_stats_backup_id_fkey1" FOREIGN KEY (id, starelid) REFERENCES dbms_stats.relation_stats_backup(id, relid) ON DELETE CASCADE
+
+-- No.2-1-5
+\d dbms_stats._relation_stats_locked
+        Table "dbms_stats._relation_stats_locked"
+      Column      |           Type           | Modifiers 
+------------------+--------------------------+-----------
+ relid            | oid                      | not null
+ relname          | text                     | not null
+ relpages         | integer                  | 
+ reltuples        | real                     | 
+ relallvisible    | integer                  | 
+ curpages         | integer                  | 
+ last_analyze     | timestamp with time zone | 
+ last_autoanalyze | timestamp with time zone | 
+Indexes:
+    "_relation_stats_locked_pkey" PRIMARY KEY, btree (relid)
+Referenced by:
+    TABLE "dbms_stats._column_stats_locked" CONSTRAINT "_column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats._relation_stats_locked(relid) ON DELETE CASCADE
+Triggers:
+    invalidate_relation_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats._relation_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache()
+
+/*
+ * No.2-2 view definitions.
+ */
+-- No.2-2-1
+\dS+ dbms_stats.column_stats_effective
+                View "dbms_stats.column_stats_effective"
+   Column    |        Type         | Modifiers | Storage  | Description 
+-------------+---------------------+-----------+----------+-------------
+ starelid    | oid                 |           | plain    | 
+ staattnum   | smallint            |           | plain    | 
+ stainherit  | boolean             |           | plain    | 
+ stanullfrac | real                |           | plain    | 
+ stawidth    | integer             |           | plain    | 
+ stadistinct | real                |           | plain    | 
+ stakind1    | smallint            |           | plain    | 
+ stakind2    | smallint            |           | plain    | 
+ stakind3    | smallint            |           | plain    | 
+ stakind4    | smallint            |           | plain    | 
+ stakind5    | smallint            |           | plain    | 
+ staop1      | oid                 |           | plain    | 
+ staop2      | oid                 |           | plain    | 
+ staop3      | oid                 |           | plain    | 
+ staop4      | oid                 |           | plain    | 
+ staop5      | oid                 |           | plain    | 
+ stanumbers1 | real[]              |           | extended | 
+ stanumbers2 | real[]              |           | extended | 
+ stanumbers3 | real[]              |           | extended | 
+ stanumbers4 | real[]              |           | extended | 
+ stanumbers5 | real[]              |           | extended | 
+ stavalues1  | dbms_stats.anyarray |           | extended | 
+ stavalues2  | dbms_stats.anyarray |           | extended | 
+ stavalues3  | dbms_stats.anyarray |           | extended | 
+ stavalues4  | dbms_stats.anyarray |           | extended | 
+ stavalues5  | dbms_stats.anyarray |           | extended | 
+View definition:
+ SELECT m.starelid,
+    m.staattnum,
+    m.stainherit,
+    m.stanullfrac,
+    m.stawidth,
+    m.stadistinct,
+    m.stakind1,
+    m.stakind2,
+    m.stakind3,
+    m.stakind4,
+    m.stakind5,
+    m.staop1,
+    m.staop2,
+    m.staop3,
+    m.staop4,
+    m.staop5,
+    m.stanumbers1,
+    m.stanumbers2,
+    m.stanumbers3,
+    m.stanumbers4,
+    m.stanumbers5,
+    m.stavalues1,
+    m.stavalues2,
+    m.stavalues3,
+    m.stavalues4,
+    m.stavalues5
+   FROM ( SELECT (dbms_stats.merge(v.*, s.*)).starelid AS starelid,
+            (dbms_stats.merge(v.*, s.*)).staattnum AS staattnum,
+            (dbms_stats.merge(v.*, s.*)).stainherit AS stainherit,
+            (dbms_stats.merge(v.*, s.*)).stanullfrac AS stanullfrac,
+            (dbms_stats.merge(v.*, s.*)).stawidth AS stawidth,
+            (dbms_stats.merge(v.*, s.*)).stadistinct AS stadistinct,
+            (dbms_stats.merge(v.*, s.*)).stakind1 AS stakind1,
+            (dbms_stats.merge(v.*, s.*)).stakind2 AS stakind2,
+            (dbms_stats.merge(v.*, s.*)).stakind3 AS stakind3,
+            (dbms_stats.merge(v.*, s.*)).stakind4 AS stakind4,
+            (dbms_stats.merge(v.*, s.*)).stakind5 AS stakind5,
+            (dbms_stats.merge(v.*, s.*)).staop1 AS staop1,
+            (dbms_stats.merge(v.*, s.*)).staop2 AS staop2,
+            (dbms_stats.merge(v.*, s.*)).staop3 AS staop3,
+            (dbms_stats.merge(v.*, s.*)).staop4 AS staop4,
+            (dbms_stats.merge(v.*, s.*)).staop5 AS staop5,
+            (dbms_stats.merge(v.*, s.*)).stanumbers1 AS stanumbers1,
+            (dbms_stats.merge(v.*, s.*)).stanumbers2 AS stanumbers2,
+            (dbms_stats.merge(v.*, s.*)).stanumbers3 AS stanumbers3,
+            (dbms_stats.merge(v.*, s.*)).stanumbers4 AS stanumbers4,
+            (dbms_stats.merge(v.*, s.*)).stanumbers5 AS stanumbers5,
+            (dbms_stats.merge(v.*, s.*)).stavalues1 AS stavalues1,
+            (dbms_stats.merge(v.*, s.*)).stavalues2 AS stavalues2,
+            (dbms_stats.merge(v.*, s.*)).stavalues3 AS stavalues3,
+            (dbms_stats.merge(v.*, s.*)).stavalues4 AS stavalues4,
+            (dbms_stats.merge(v.*, s.*)).stavalues5 AS stavalues5
+           FROM pg_statistic s
+             FULL JOIN dbms_stats._column_stats_locked v USING (starelid, staattnum, stainherit)
+          WHERE NOT dbms_stats.is_system_catalog(starelid::regclass) AND (EXISTS ( SELECT NULL::unknown
+                   FROM pg_attribute a
+                  WHERE a.attrelid = starelid AND a.attnum = staattnum AND a.attisdropped = false))) m
+  WHERE m.starelid IS NOT NULL;
+
+-- No.2-2-2
+\dS+ dbms_stats.relation_stats_effective
+                    View "dbms_stats.relation_stats_effective"
+      Column      |           Type           | Modifiers | Storage  | Description 
+------------------+--------------------------+-----------+----------+-------------
+ relid            | oid                      |           | plain    | 
+ relname          | text                     |           | extended | 
+ relpages         | integer                  |           | plain    | 
+ reltuples        | real                     |           | plain    | 
+ relallvisible    | integer                  |           | plain    | 
+ curpages         | integer                  |           | plain    | 
+ last_analyze     | timestamp with time zone |           | plain    | 
+ last_autoanalyze | timestamp with time zone |           | plain    | 
+View definition:
+ SELECT c.oid AS relid,
+    dbms_stats.relname(n.nspname::text, c.relname::text) AS relname,
+    COALESCE(v.relpages, c.relpages) AS relpages,
+    COALESCE(v.reltuples, c.reltuples) AS reltuples,
+    COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
+    COALESCE(v.curpages, (pg_relation_size(c.oid::regclass) / current_setting('block_size'::text)::integer)::integer) AS curpages,
+    COALESCE(v.last_analyze, pg_stat_get_last_analyze_time(c.oid)) AS last_analyze,
+    COALESCE(v.last_autoanalyze, pg_stat_get_last_autoanalyze_time(c.oid)) AS last_autoanalyze
+   FROM pg_class c
+     JOIN pg_namespace n ON c.relnamespace = n.oid
+     LEFT JOIN dbms_stats._relation_stats_locked v ON v.relid = c.oid
+  WHERE dbms_stats.is_target_relkind(c.relkind) AND NOT dbms_stats.is_system_schema(n.nspname::text);
+
+-- No.2-2-3
+\dS+ dbms_stats.stats
+                              View "dbms_stats.stats"
+         Column         |        Type         | Modifiers | Storage  | Description 
+------------------------+---------------------+-----------+----------+-------------
+ schemaname             | name                |           | plain    | 
+ tablename              | name                |           | plain    | 
+ attname                | name                |           | plain    | 
+ inherited              | boolean             |           | plain    | 
+ null_frac              | real                |           | plain    | 
+ avg_width              | integer             |           | plain    | 
+ n_distinct             | real                |           | plain    | 
+ most_common_vals       | dbms_stats.anyarray |           | extended | 
+ most_common_freqs      | real[]              |           | extended | 
+ histogram_bounds       | dbms_stats.anyarray |           | extended | 
+ correlation            | real                |           | plain    | 
+ most_common_elems      | dbms_stats.anyarray |           | extended | 
+ most_common_elem_freqs | real[]              |           | extended | 
+ elem_count_histogram   | real[]              |           | extended | 
+View definition:
+ SELECT n.nspname AS schemaname,
+    c.relname AS tablename,
+    a.attname,
+    s.stainherit AS inherited,
+    s.stanullfrac AS null_frac,
+    s.stawidth AS avg_width,
+    s.stadistinct AS n_distinct,
+        CASE
+            WHEN s.stakind1 = 1 THEN s.stavalues1
+            WHEN s.stakind2 = 1 THEN s.stavalues2
+            WHEN s.stakind3 = 1 THEN s.stavalues3
+            WHEN s.stakind4 = 1 THEN s.stavalues4
+            WHEN s.stakind5 = 1 THEN s.stavalues5
+            ELSE NULL::dbms_stats.anyarray
+        END AS most_common_vals,
+        CASE
+            WHEN s.stakind1 = 1 THEN s.stanumbers1
+            WHEN s.stakind2 = 1 THEN s.stanumbers2
+            WHEN s.stakind3 = 1 THEN s.stanumbers3
+            WHEN s.stakind4 = 1 THEN s.stanumbers4
+            WHEN s.stakind5 = 1 THEN s.stanumbers5
+            ELSE NULL::real[]
+        END AS most_common_freqs,
+        CASE
+            WHEN s.stakind1 = 2 THEN s.stavalues1
+            WHEN s.stakind2 = 2 THEN s.stavalues2
+            WHEN s.stakind3 = 2 THEN s.stavalues3
+            WHEN s.stakind4 = 2 THEN s.stavalues4
+            WHEN s.stakind5 = 2 THEN s.stavalues5
+            ELSE NULL::dbms_stats.anyarray
+        END AS histogram_bounds,
+        CASE
+            WHEN s.stakind1 = 3 THEN s.stanumbers1[1]
+            WHEN s.stakind2 = 3 THEN s.stanumbers2[1]
+            WHEN s.stakind3 = 3 THEN s.stanumbers3[1]
+            WHEN s.stakind4 = 3 THEN s.stanumbers4[1]
+            WHEN s.stakind5 = 3 THEN s.stanumbers5[1]
+            ELSE NULL::real
+        END AS correlation,
+        CASE
+            WHEN s.stakind1 = 4 THEN s.stavalues1
+            WHEN s.stakind2 = 4 THEN s.stavalues2
+            WHEN s.stakind3 = 4 THEN s.stavalues3
+            WHEN s.stakind4 = 4 THEN s.stavalues4
+            WHEN s.stakind5 = 4 THEN s.stavalues5
+            ELSE NULL::dbms_stats.anyarray
+        END AS most_common_elems,
+        CASE
+            WHEN s.stakind1 = 4 THEN s.stanumbers1
+            WHEN s.stakind2 = 4 THEN s.stanumbers2
+            WHEN s.stakind3 = 4 THEN s.stanumbers3
+            WHEN s.stakind4 = 4 THEN s.stanumbers4
+            WHEN s.stakind5 = 4 THEN s.stanumbers5
+            ELSE NULL::real[]
+        END AS most_common_elem_freqs,
+        CASE
+            WHEN s.stakind1 = 5 THEN s.stanumbers1
+            WHEN s.stakind2 = 5 THEN s.stanumbers2
+            WHEN s.stakind3 = 5 THEN s.stanumbers3
+            WHEN s.stakind4 = 5 THEN s.stanumbers4
+            WHEN s.stakind5 = 5 THEN s.stanumbers5
+            ELSE NULL::real[]
+        END AS elem_count_histogram
+   FROM dbms_stats.column_stats_effective s
+     JOIN pg_class c ON c.oid = s.starelid
+     JOIN pg_attribute a ON c.oid = a.attrelid AND a.attnum = s.staattnum
+     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
+  WHERE NOT a.attisdropped AND has_column_privilege(c.oid, a.attnum, 'select'::text);
+
+-- No.2-2-4
+\dS+ dbms_stats.column_stats_locked
+                 View "dbms_stats.column_stats_locked"
+   Column    |        Type         | Modifiers | Storage  | Description 
+-------------+---------------------+-----------+----------+-------------
+ starelid    | oid                 |           | plain    | 
+ staattnum   | smallint            |           | plain    | 
+ stainherit  | boolean             |           | plain    | 
+ stanullfrac | real                |           | plain    | 
+ stawidth    | integer             |           | plain    | 
+ stadistinct | real                |           | plain    | 
+ stakind1    | smallint            |           | plain    | 
+ stakind2    | smallint            |           | plain    | 
+ stakind3    | smallint            |           | plain    | 
+ stakind4    | smallint            |           | plain    | 
+ stakind5    | smallint            |           | plain    | 
+ staop1      | oid                 |           | plain    | 
+ staop2      | oid                 |           | plain    | 
+ staop3      | oid                 |           | plain    | 
+ staop4      | oid                 |           | plain    | 
+ staop5      | oid                 |           | plain    | 
+ stanumbers1 | real[]              |           | extended | 
+ stanumbers2 | real[]              |           | extended | 
+ stanumbers3 | real[]              |           | extended | 
+ stanumbers4 | real[]              |           | extended | 
+ stanumbers5 | real[]              |           | extended | 
+ stavalues1  | dbms_stats.anyarray |           | extended | 
+ stavalues2  | dbms_stats.anyarray |           | extended | 
+ stavalues3  | dbms_stats.anyarray |           | extended | 
+ stavalues4  | dbms_stats.anyarray |           | extended | 
+ stavalues5  | dbms_stats.anyarray |           | extended | 
+View definition:
+ SELECT _column_stats_locked.starelid,
+    _column_stats_locked.staattnum,
+    _column_stats_locked.stainherit,
+    _column_stats_locked.stanullfrac,
+    _column_stats_locked.stawidth,
+    _column_stats_locked.stadistinct,
+    _column_stats_locked.stakind1,
+    _column_stats_locked.stakind2,
+    _column_stats_locked.stakind3,
+    _column_stats_locked.stakind4,
+    _column_stats_locked.stakind5,
+    _column_stats_locked.staop1,
+    _column_stats_locked.staop2,
+    _column_stats_locked.staop3,
+    _column_stats_locked.staop4,
+    _column_stats_locked.staop5,
+    _column_stats_locked.stanumbers1,
+    _column_stats_locked.stanumbers2,
+    _column_stats_locked.stanumbers3,
+    _column_stats_locked.stanumbers4,
+    _column_stats_locked.stanumbers5,
+    _column_stats_locked.stavalues1,
+    _column_stats_locked.stavalues2,
+    _column_stats_locked.stavalues3,
+    _column_stats_locked.stavalues4,
+    _column_stats_locked.stavalues5
+   FROM dbms_stats._column_stats_locked
+  WHERE has_column_privilege(_column_stats_locked.starelid, _column_stats_locked.staattnum, 'SELECT'::text);
+
+-- No.2-2-5
+\dS+ dbms_stats.relation_stats_locked
+                     View "dbms_stats.relation_stats_locked"
+      Column      |           Type           | Modifiers | Storage  | Description 
+------------------+--------------------------+-----------+----------+-------------
+ relid            | oid                      |           | plain    | 
+ relname          | text                     |           | extended | 
+ relpages         | integer                  |           | plain    | 
+ reltuples        | real                     |           | plain    | 
+ relallvisible    | integer                  |           | plain    | 
+ curpages         | integer                  |           | plain    | 
+ last_analyze     | timestamp with time zone |           | plain    | 
+ last_autoanalyze | timestamp with time zone |           | plain    | 
+View definition:
+ SELECT _relation_stats_locked.relid,
+    _relation_stats_locked.relname,
+    _relation_stats_locked.relpages,
+    _relation_stats_locked.reltuples,
+    _relation_stats_locked.relallvisible,
+    _relation_stats_locked.curpages,
+    _relation_stats_locked.last_analyze,
+    _relation_stats_locked.last_autoanalyze
+   FROM dbms_stats._relation_stats_locked;
+
+/*
+ * No.2-4 dbms_stats.anyarray
+ */
+-- No.2-4-1
+SELECT n.nspname, t.typname, t.typlen, t.typbyval, t.typtype,
+       t.typcategory, t.typispreferred, t.typisdefined, t.typdelim,
+       t.typrelid, t.typelem, t.typinput, t.typoutput, t.typreceive,
+       t.typsend, t.typmodin, t.typmodout, t.typanalyze, t.typalign,
+       t.typstorage, t.typnotnull, t.typbasetype, t.typtypmod, t.typndims,
+       t.typcollation, t.typdefaultbin, t.typdefault, t.typacl
+  FROM pg_type t, pg_namespace n
+ WHERE t.typnamespace = n.oid
+   AND n.nspname = 'dbms_stats'
+   AND t.typname = 'anyarray';
+  nspname   | typname  | typlen | typbyval | typtype | typcategory | typispreferred | typisdefined | typdelim | typrelid | typelem |        typinput        |        typoutput        |        typreceive        |         typsend          | typmodin | typmodout | typanalyze | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault | typacl 
+------------+----------+--------+----------+---------+-------------+----------------+--------------+----------+----------+---------+------------------------+-------------------------+--------------------------+--------------------------+----------+-----------+------------+----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------+--------
+ dbms_stats | anyarray |     -1 | f        | b       | P           | f              | t            | ,        |        0 |       0 | dbms_stats.anyarray_in | dbms_stats.anyarray_out | dbms_stats.anyarray_recv | dbms_stats.anyarray_send | -        | -         | -          | d        | x          | f          |           0 |        -1 |        0 |            0 | (null)        | (null)     | (null)
+(1 row)
+
+/*
+ * No.5-1 dbms_stats.merge
+ */
+UPDATE pg_statistic SET
+    stanullfrac = staattnum,
+    stawidth = staattnum,
+    stadistinct = staattnum,
+    stakind1 = 4,
+    stakind2 = 1,
+    stakind3 = 2,
+    stakind4 = 3,
+    stakind5 = 5,
+    staop1 = 14,
+    staop2 = 11,
+    staop3 = 12,
+    staop4 = 13,
+    staop5 = 15,
+    stanumbers1 = ARRAY[staattnum,4],
+    stanumbers2 = ARRAY[staattnum,1],
+    stanumbers3 = ARRAY[staattnum,2],
+    stanumbers4 = ARRAY[staattnum,3],
+    stanumbers5 = ARRAY[staattnum,5],
+    stavalues2 = array_cat(stavalues1,stavalues1),
+    stavalues3 = array_cat(array_cat(stavalues1,stavalues1),stavalues1),
+    stavalues4 = array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1)
+   ,stavalues5 = array_cat(array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1),stavalues1)
+ WHERE starelid = 'st0'::regclass;
+SELECT dbms_stats.lock_table_stats('st0');
+ lock_table_stats 
+------------------
+ st0
+(1 row)
+
+UPDATE dbms_stats._column_stats_locked SET
+    stainherit = 't',
+    stanullfrac = -staattnum,
+    stawidth = -staattnum,
+    stadistinct = -staattnum,
+    stakind1 = 2,
+    stakind2 = 3,
+    stakind3 = 4,
+    stakind4 = 1,
+    stakind5 = 5,
+    staop1 = 22,
+    staop2 = 23,
+    staop3 = 24,
+    staop4 = 21,
+    staop5 = 25,
+    stanumbers1 = ARRAY[-staattnum,22],
+    stanumbers2 = ARRAY[-staattnum,23],
+    stanumbers3 = ARRAY[-staattnum,24],
+    stanumbers4 = ARRAY[-staattnum,21],
+    stanumbers5 = ARRAY[-staattnum,25],
+    stavalues1 = stavalues3,
+    stavalues2 = stavalues2,
+    stavalues3 = stavalues1,
+    stavalues4 = stavalues4
+   ,stavalues5 = stavalues5
+;
+/*
+ * Driver function dbms_stats.merge1
+ */
+CREATE FUNCTION dbms_stats.merge1(
+    lhs dbms_stats._column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS integer AS
+'$libdir/pg_dbms_stats', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+SELECT * FROM columns_locked_v
+ WHERE starelid = 'st0'::regclass;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         1 | id      | t          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | {1,2,1,2,1,2}             | {1,2,1,2}         | {1,2}      | {1,2,1,2,1,2,1,2}                 | {1,2,1,2,1,2,1,2,1,2}
+ st0      |         2 | name    | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(2 rows)
+
+SELECT * FROM plain_columns_statistic_v
+ WHERE starelid = 'st0'::regclass;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 |    stavalues2     |        stavalues3         |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+-------------------+---------------------------+-----------------------------------+-------------------------------------------
+ st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}         | {1,2,1,2,1,2}             | {1,2,1,2,1,2,1,2}                 | {1,2,1,2,1,2,1,2,1,2}
+ st0      |         2 | f          |           2 |        2 |           2 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {2,4}       | {2,1}       | {2,2}       | {2,3}       | {2,5}       | {"test "}  | {"test ","test "} | {"test ","test ","test "} | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(2 rows)
+
+SET client_min_messages TO LOG;
+-- No.5-1-1
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(NULL, s)
+         FROM pg_statistic s
+        WHERE starelid = 'st0'::regclass
+          AND staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
+ st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
+(1 row)
+
+-- No.5-1-2
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, NULL)
+         FROM dbms_stats._column_stats_locked v
+        WHERE starelid = 'st0'::regclass
+          AND staattnum = '2'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-3
+SELECT dbms_stats.merge(NULL, NULL);
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-4
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-5
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-6
+SELECT dbms_stats.merge1(v, s)
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+ERROR:  return type must be a row type
+-- No.5-1-7
+SELECT dbms_stats.merge(NULL, (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM pg_statistic s
+ WHERE s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-8
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(NULL, (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM pg_statistic s
+        WHERE s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
+ st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | (null)      | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
+(1 row)
+
+-- No.5-1-9
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), NULL)
+  FROM dbms_stats._column_stats_locked v
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2;
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-10
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), NULL)
+         FROM dbms_stats._column_stats_locked v
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | (null)      | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-11
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+LOG:  pg_dbms_stats: bad statistics
+DETAIL:  column "staop5" should not be null
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-12
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | (null)      | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-13
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+             NULL, NULL, NULL,
+             NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL
+            ,NULL, NULL, NULL, NULL
+             ), s)
+         FROM pg_statistic s
+        WHERE s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
+ st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
+(1 row)
+
+-- No.5-1-14
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, (
+             NULL, NULL, NULL,
+             NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL))
+         FROM dbms_stats._column_stats_locked v
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-15
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-16
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL
+      ,NULL, NULL, NULL, NULL
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+LOG:  pg_dbms_stats: bad statistics
+DETAIL:  column "stakind1" should not be null
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-17
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3,
+              v.staop4,
+              NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
+ st0      |         1 | t          |          -1 |       -1 |          -1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
+(1 row)
+
+-- No.5-1-18
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '1'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+LOG:  pg_dbms_stats: bad statistics
+DETAIL:  column "staop5" should not be null
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-19
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        1 |        1 |        1 |        1 |        1 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-20
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+ starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
+ st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        2 |        2 |        2 |        2 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
+(1 row)
+
+-- No.5-1-21
+SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2;
+LOG:  pg_dbms_stats: bad column type
+DETAIL:  type of column "name" has been changed
+HINT:  need to execute dbms_stats.unlock('st0', 'name')
+ merge  
+--------
+ (null)
+(1 row)
+
+-- No.5-1-22
+SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2;
+LOG:  pg_dbms_stats: bad column type
+DETAIL:  type of column "name" has been changed
+HINT:  need to execute dbms_stats.unlock('st0', 'name')
+ merge  
+--------
+ (null)
+(1 row)
+
+RESET client_min_messages;
+SELECT dbms_stats.unlock_database_stats();
+ unlock_database_stats 
+-----------------------
+ st0
+(1 row)
+
+/*
+ * No.6-4 dbms_stats.is_target_relkind
+ */
+-- No.6-4- 10-11
+SELECT dbms_stats.is_target_relkind(k::"char")
+ FROM (VALUES ('r'), ('i'), ('f'), ('m'),
+                         ('S'), ('t'), ('v'), ('c')) t(k);
+ is_target_relkind 
+-------------------
+ t
+ t
+ t
+ t
+ f
+ f
+ f
+ f
+(8 rows)
+
+/*
+ * No.7-1 dbms_stats.backup
+ */
+DELETE FROM dbms_stats.backup_history;
+INSERT INTO dbms_stats.backup_history(id, time, unit) values(1, '2012-01-01', 'd');
+-- No.7-1-9
+SELECT dbms_stats.backup(1, 's0.sft0'::regclass, NULL);
+ backup 
+--------
+      1
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+     1
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+     1
+(1 row)
+
+-- No.7-1-10
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, 's0.smv0'::regclass, NULL);
+ backup 
+--------
+      1
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+     1
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+     3
+(1 row)
+
+-- No.7-1-12
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, NULL, 1::int2);
+ backup 
+--------
+      1
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_backup
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s1.st0
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st1      |         1
+ s0.st0   |         1
+ s0.st1   |         1
+ s0.st2   |         1
+ s1.st0   |         1
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+(9 rows)
+
+-- No.7-1-14
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, NULL::regclass, NULL);
+ backup 
+--------
+      1
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_backup
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s1.st0
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st0      |         2
+ st1      |         1
+ st1      |         2
+ s0.st0   |         1
+ s0.st0   |         2
+ s0.st1   |         1
+ s0.st1   |         2
+ s0.st2   |         1
+ s0.st2   |         2
+ s1.st0   |         1
+ s1.st0   |         2
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+ s0.smv0  |         2
+ s0.smv0  |         3
+(17 rows)
+
+-- No.7-1-18
+DELETE FROM dbms_stats.relation_stats_backup;
+\! psql contrib_regression -c "SELECT dbms_stats.backup(NULL, 's0.st0'::regclass, NULL)" > results/ut_no2_1_17.out 2>&1
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+     0
+(1 row)
+
+/*
+ * No.8-1 dbms_stats.backup
+ */
+SELECT setval('dbms_stats.backup_history_id_seq',1, false);
+ setval 
+--------
+      1
+(1 row)
+
+/*
+ * Stab function dbms_stats.backup
+ */
+ALTER FUNCTION dbms_stats.backup(
+    backup_id int8,
+    relid regclass,
+    attnum int2)
+    RENAME TO truth_func_backup;
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    backup_id int8,
+    regclass,
+    attnum int2)
+RETURNS int8 AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
+    RETURN 1;
+END;
+$$
+LANGUAGE plpgsql;
+-- No.8-1-1
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
+NOTICE:  arguments are 1, s0.st0, 1
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  1 | c    | dummy comment
+(1 row)
+
+-- No.8-1-2
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 2, s0.st0, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  2 | t    | dummy comment
+(1 row)
+
+-- No.8-1-3
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(NULL::regclass, 'id', 'dummy comment');
+ERROR:  relation required
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-4
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(NULL::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 3, <NULL>, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  3 | d    | dummy comment
+(1 row)
+
+-- No.8-1-5
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(0, NULL, 'dummy comment');
+ERROR:  relation "-" not found
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-6
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 4, s0.st0, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  4 | t    | dummy comment
+(1 row)
+
+-- No.8-1-7
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(
+    'pg_toast.pg_toast_2618'::regclass,
+    NULL,
+    'dummy comment');
+ERROR:  relation of relkind "t" cannot have statistics to backup: "pg_toast.pg_toast_2618"
+HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-8
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0_idx'::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 5, s0.st0_idx, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  5 | t    | dummy comment
+(1 row)
+
+-- No.8-1-9
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.ss0'::regclass, NULL, 'dummy comment');
+ERROR:  relation of relkind "S" cannot have statistics to backup: "s0.ss0"
+HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-10
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.sc0'::regclass, NULL, 'dummy comment');
+ERROR:  relation of relkind "c" cannot have statistics to backup: "s0.sc0"
+HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-11
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.sft0'::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 6, s0.sft0, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  6 | t    | dummy comment
+(1 row)
+
+-- No.8-1-12
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.smv0'::regclass, NULL, 'dummy comment');
+NOTICE:  arguments are 7, s0.smv0, <NULL>
+CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum)"
+PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
+ backup 
+--------
+      1
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit |    comment    
+----+------+---------------
+  7 | t    | dummy comment
+(1 row)
+
+-- No.8-1-13
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('pg_catalog.pg_class'::regclass, NULL, 'dummy comment');
+ERROR:  backing up statistics is inhibited for system catalogs: "pg_class"
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-14
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, 'dummy', 'dummy comment');
+ERROR:  column "dummy" not found in relation "s0.st0"
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+-- No.8-1-15
+DELETE FROM dbms_stats.backup_history;
+DELETE FROM pg_statistic
+ WHERE starelid = 's0.st0'::regclass
+   AND staattnum = 1::int2;
+SELECT count(*) FROM dbms_stats.column_stats_effective
+ WHERE starelid = 's0.st0'::regclass
+   AND staattnum = 1::int2;
+ count 
+-------
+     0
+(1 row)
+
+SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
+ERROR:  no statistics available for column "id" of relation "s0.st0"
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+ id | unit | comment 
+----+------+---------
+(0 rows)
+
+/*
+ * Stab function dbms_stats.backup
+ */
+ALTER FUNCTION dbms_stats.backup(
+    relid regclass,
+    attname text,
+    comment text)
+    RENAME TO truth_func_backup;
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL,
+    comment text DEFAULT NULL)
+RETURNS int8 AS
+$$
+BEGIN
+    IF $3 = '<NULL>' THEN
+        RAISE NOTICE 'third argument is not NULL but string "<NULL>"';
+    END IF;
+    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
+    RETURN 1;
+END;
+$$
+LANGUAGE plpgsql;
+/*
+ * No.8-3 dbms_stats.backup_schema_stats
+ */
+SELECT setval('dbms_stats.backup_history_id_seq',9, false);
+ setval 
+--------
+      9
+(1 row)
+
+-- No.8-3-1
+SELECT dbms_stats.backup_schema_stats('s0', 'comment');
+NOTICE:  arguments are 9, s0.st0, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.st0_idx, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.st1, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.st1_idx, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.st2, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.st2_idx, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.sft0, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+NOTICE:  arguments are 9, s0.smv0, <NULL>
+CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn"
+PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
+ backup_schema_stats 
+---------------------
+                   9
+(1 row)
+
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+ id | unit | comment 
+----+------+---------
+  9 | s    | comment
+(1 row)
+
+-- No.8-3-2
+SELECT dbms_stats.backup_schema_stats('s00', 'comment');
+ERROR:  schema "s00" not found
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+ id | unit | comment 
+----+------+---------
+  9 | s    | comment
+(1 row)
+
+-- No.8-3-3
+SELECT dbms_stats.backup_schema_stats('pg_catalog', 'comment');
+ERROR:  backing up statistics is inhibited for system schemas: "pg_catalog"
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+ id | unit | comment 
+----+------+---------
+  9 | s    | comment
+(1 row)
+
+/*
+ * Delete stab function dbms_stats.backup
+ */
+DROP FUNCTION dbms_stats.backup(
+    backup_id int8,
+    regclass,
+    attnum int2);
+ALTER FUNCTION dbms_stats.truth_func_backup(
+    backup_id int8,
+    regclass,
+    attnum int2)
+    RENAME TO backup;
+DROP FUNCTION dbms_stats.backup(
+    regclass,
+    attname text,
+    comment text);
+ALTER FUNCTION dbms_stats.truth_func_backup(
+    regclass,
+    attname text,
+    comment text)
+    RENAME TO backup;
+VACUUM ANALYZE;
+/*
+ * create backup statistics state A
+ */
+DELETE FROM dbms_stats.backup_history;
+INSERT INTO dbms_stats.backup_history(id, time, unit)
+    VALUES (1, '2012-02-29 23:59:56.999999', 'd');
+SELECT setval('dbms_stats.backup_history_id_seq',1);
+ setval 
+--------
+      1
+(1 row)
+
+SELECT dbms_stats.backup();
+ backup 
+--------
+      2
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:57'
+ WHERE id = 2;
+SELECT dbms_stats.backup('s0.st0');
+ backup 
+--------
+      3
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:57.000001'
+ WHERE id = 3;
+SELECT dbms_stats.backup();
+ backup 
+--------
+      4
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:58'
+ WHERE id = 4;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 4;
+SELECT dbms_stats.backup('s0.st0', 'id');
+ backup 
+--------
+      5
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:00'
+ WHERE id = 5;
+SELECT dbms_stats.backup('s0.st0');
+ backup 
+--------
+      6
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:02'
+ WHERE id = 6;
+SELECT dbms_stats.backup('public.st0');
+ backup 
+--------
+      7
+(1 row)
+
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:04'
+ WHERE id = 7;
+INSERT INTO dbms_stats.backup_history(time, unit)
+    VALUES ('2012-03-01 00:00:06', 's');
+SELECT dbms_stats.backup(8, c.oid, NULL)
+  FROM pg_catalog.pg_class c,
+       pg_catalog.pg_namespace n
+ WHERE n.nspname = 's0'
+   AND c.relnamespace = n.oid
+   AND c.relkind IN ('r', 'i');
+ backup 
+--------
+      8
+      8
+      8
+      8
+      8
+      8
+(6 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT * FROM relations_backup_v;
+ id |   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
+----+------------+----------------+----------+-----------+---------------+----------
+  2 | pt0        | public.pt0     |        0 |         0 |             0 |        0
+  2 | pt0_idx    | public.pt0_idx |        2 |         0 |             0 |        2
+  2 | st0        | public.st0     |        1 |         2 |             1 |        1
+  2 | st0_idx    | public.st0_idx |        2 |         2 |             0 |        2
+  2 | st1        | public.st1     |       45 |     10000 |            45 |       45
+  2 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+  2 | s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
+  2 | s0.st1     | s0.st1         |        1 |         3 |             1 |        1
+  2 | s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
+  2 | s0.st2     | s0.st2         |        1 |         3 |             1 |        1
+  2 | s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
+  2 | s1.st0     | s1.st0         |        1 |         4 |             1 |        1
+  2 | st1_idx    | public.st1_idx |       30 |     10000 |             0 |       30
+  2 | st1_exp    | public.st1_exp |       30 |     10000 |             0 |       30
+  2 | s0.sft0    | s0.sft0        |        1 |        10 |             0 |        0
+  2 | s0.smv0    | s0.smv0        |        1 |         2 |             1 |        1
+  3 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+  5 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+  6 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+  7 | st0        | public.st0     |        1 |         2 |             1 |        1
+  8 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+  8 | s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
+  8 | s0.st1     | s0.st1         |        1 |         3 |             1 |        1
+  8 | s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
+  8 | s0.st2     | s0.st2         |        1 |         3 |             1 |        1
+  8 | s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
+(26 rows)
+
+SELECT * FROM columns_backup_v;
+ id | statypid | starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 |      stanumbers1       | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----+----------+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+------------------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+  2 |       23 | st0      |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  2 |     1042 | st0      |         2 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}                    | {1}         | (null)      | (null)      | (null)      | {"test "}              | (null)     | (null)     | (null)     | (null)
+  2 |       23 | st1      |         1 | f          |           0 |        4 |           3 |        1 |        3 |        0 |        0 |        0 |     96 |     97 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+  2 |       25 | st1      |         2 | f          |           0 |        2 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st1   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st1   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.st2   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
+  2 |       25 | s0.st2   |         2 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s1.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4}              | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s1.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {15,25,35,45}          | (null)     | (null)     | (null)     | (null)
+  2 |       25 | st1_exp  |         1 | f          |           0 |        5 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.sft0  |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.smv0  |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  2 |       23 | s0.smv0  |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+  2 |       25 | s0.smv0  |         3 | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,test}               | (null)     | (null)     | (null)     | (null)
+  3 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  3 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+  3 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+  3 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+  5 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  5 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+  6 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  6 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+  6 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+  6 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+  7 |       23 | st0      |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  7 |     1042 | st0      |         2 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}                    | {1}         | (null)      | (null)      | (null)      | {"test "}              | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st1   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st1   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
+  8 |       23 | s0.st2   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
+  8 |       25 | s0.st2   |         2 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
+(39 rows)
+
+VACUUM ANALYZE;
+/*
+ * No.9-1 dbms_stats.restore
+ */
+-- No.9-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats.backup_history         | RowShareLock
+ dbms_stats.column_stats_backup    | AccessShareLock
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats.relation_stats_backup  | AccessShareLock
+ dbms_stats.relation_stats_backup  | RowShareLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(7 rows)
+
+COMMIT;
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid  
+--------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+(4 rows)
+
+-- No.9-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 'st0', NULL);
+ restore 
+---------
+ st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+ st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+(2 rows)
+
+-- No.9-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's00.s0', NULL);
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.restore(2, 's00.s0', NULL);
+                                     ^
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(NULL, 's0.st0', NULL);
+ERROR:  backup id required
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', 'id');
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid  
+--------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+(2 rows)
+
+-- No.9-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, NULL, 'id');
+ERROR:  relation required
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid  
+--------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+(4 rows)
+
+-- No.9-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, NULL, NULL);
+  restore   
+------------
+ pt0
+ pt0_idx
+ s0.sft0
+ s0.smv0
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s1.st0
+ st0
+ st0_idx
+ st1
+ st1_exp
+ st1_idx
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s1.st0
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+(19 rows)
+
+-- No.9-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(0, 's0.st0', NULL);
+ERROR:  backup id 0 not found
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+(0 rows)
+
+-- No.9-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 0, 'id');
+ERROR:  relation "-" not found
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(1, 's0.st0', NULL);
+ERROR:  statistics of relation "s0.st0" not found in any backups before backup id = 1
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', 'dummy');
+ERROR:  column "dummy" not found in relation s0.st0
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(1, 's0.st0', 'id');
+ERROR:  statistics of relation "s0.st0" not found in any backups before backup id = 1
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.9-1-15
+DELETE FROM dbms_stats._relation_stats_locked;
+ALTER TABLE s1.st0 DROP COLUMN id;
+SELECT dbms_stats.restore(2, 's1.st0', 'id');
+ERROR:  column "id" not found in relation s1.st0
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+(0 rows)
+
+-- No.9-1-14
+DELETE FROM dbms_stats._relation_stats_locked;
+\set s1_st0_oid `psql contrib_regression -tA -c "SELECT c.oid FROM pg_class c, pg_namespace n WHERE c.relnamespace = n.oid AND n.nspname = 's1' AND c.relname = 'st0';"`
+DROP TABLE s1.st0;
+-- SELECT dbms_stats.restore(2, :s1_st0_oid, NULL);
+-- To avoid test unstability caused by relation id alloction, the test
+-- above is omitted.
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+CREATE TABLE s1.st0(id integer, num integer);
+INSERT INTO s1.st0 VALUES (1, 15), (2, 25), (3, 35), (4, 45);
+VACUUM ANALYZE;
+-- No.9-1-16
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid  
+--------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+(4 rows)
+
+-- No.9-1-17
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2,
+             'pg_toast.pg_toast_2618'::regclass,
+             'pg_toast.pg_toast_2618', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 'pg_toast.pg_toast_2618';
+ id |         relid          |        relname         | relpages | reltuples | relallvisible | curpages 
+----+------------------------+------------------------+----------+-----------+---------------+----------
+  2 | pg_toast.pg_toast_2618 | pg_toast.pg_toast_2618 |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 'pg_toast.pg_toast_2618', NULL);
+ restore 
+---------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 'pg_toast.pg_toast_2618';
+-- No.9-1-18
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0_idx', NULL);
+  restore   
+------------
+ s0.st0_idx
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ s0.st0_idx
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+(0 rows)
+
+-- No.9-1-19
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 's0.ss0'::regclass, 's0.ss0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 's0.ss0';
+ id | relid  | relname | relpages | reltuples | relallvisible | curpages 
+----+--------+---------+----------+-----------+---------------+----------
+  2 | s0.ss0 | s0.ss0  |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.ss0', NULL);
+ restore 
+---------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 's0.ss0';
+-- No.9-1-20
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 's0.sc0'::regclass, 's0.sc0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 's0.sc0';
+ id | relid  | relname | relpages | reltuples | relallvisible | curpages 
+----+--------+---------+----------+-----------+---------------+----------
+  2 | s0.sc0 | s0.sc0  |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.sc0', NULL);
+ restore 
+---------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 's0.sc0';
+-- No.9-1-21
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (3, 's0.sft0'::regclass, 's0.sft0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 3
+   AND relname = 's0.sft0';
+ id |  relid  | relname | relpages | reltuples | relallvisible | curpages 
+----+---------+---------+----------+-----------+---------------+----------
+  3 | s0.sft0 | s0.sft0 |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.sft0', NULL);
+ restore 
+---------
+ s0.sft0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     1
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     1
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 3
+   AND relname = 's0.sft0';
+-- No.9-1-22
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (3, 's0.smv0'::regclass, 's0.smv0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 3
+   AND relname = 's0.smv0';
+ id |  relid  | relname | relpages | reltuples | relallvisible | curpages 
+----+---------+---------+----------+-----------+---------------+----------
+  3 | s0.smv0 | s0.smv0 |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.smv0', NULL);
+ restore 
+---------
+ s0.smv0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     3
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     1
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 3
+   AND relname = 's0.smv0';
+-- No.9-1-23
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 'pg_catalog.pg_class'::regclass, 'pg_catalog.pg_class', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 'pg_catalog.pg_class';
+ id |  relid   |       relname       | relpages | reltuples | relallvisible | curpages 
+----+----------+---------------------+----------+-----------+---------------+----------
+  2 | pg_class | pg_catalog.pg_class |        1 |         1 |             1 |        1
+(1 row)
+
+SELECT dbms_stats.restore(2, 'pg_catalog.pg_class', NULL);
+ restore 
+---------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 'pg_catalog.pg_class';
+-- No.9-1-24
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(relid, relname)
+    VALUES ('s0.st0'::regclass, 's0.st0');
+INSERT INTO dbms_stats._column_stats_locked(starelid, staattnum, stainherit)
+     SELECT starelid::regclass, staattnum, stainherit
+       FROM dbms_stats.column_stats_effective
+      WHERE starelid = 's0.st0'::regclass;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 2;
+ id | unit | comment 
+----+------+---------
+  2 | d    | (null)
+(1 row)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+-- No.9-1-25
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 2;
+ id | unit | comment 
+----+------+---------
+  2 | d    | (null)
+(1 row)
+
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+ restore 
+---------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+/*
+ * Stab function dbms_stats.restore
+ */
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL)
+RETURNS SETOF regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are "%, %, %"', $1, $2, $3;
+    RETURN QUERY
+        SELECT c.oid::regclass
+          FROM pg_class c, dbms_stats.relation_stats_backup b
+         WHERE (c.oid = $2 OR $2 IS NULL)
+           AND c.oid = b.relid
+           AND c.relkind IN ('r', 'i')
+           AND (b.id <= $1 OR $1 IS NOT NULL)
+         GROUP BY c.oid
+         ORDER BY c.oid::regclass::text;
+END;
+$$
+LANGUAGE plpgsql;
+/*
+ * No.10-1 dbms_stats.restore_database_stats
+ */
+-- No.10-1-1
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
+NOTICE:  arguments are "2, pt0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, pt0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_exp, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+ restore_database_stats 
+------------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+(13 rows)
+
+-- No.10-1-2
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57.000002');
+NOTICE:  arguments are "2, pt0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, pt0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "3, s0.st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_exp, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+ restore_database_stats 
+------------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+(13 rows)
+
+-- No.10-1-3
+SELECT dbms_stats.restore_database_stats('2012-01-01 00:00:00');
+ restore_database_stats 
+------------------------
+(0 rows)
+
+--#No.10-1-4 is skipped after lock tests
+--#No.10-1-5 is skipped after lock tests
+-- No.10-1-6
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
+NOTICE:  arguments are "2, pt0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, pt0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_idx, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, st1_exp, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  SQL function "restore_database_stats" statement 1
+ restore_database_stats 
+------------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+(13 rows)
+
+/*
+ * No.10-2 dbms_stats.restore_schema_stats
+ */
+-- No.10-2-1
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+NOTICE:  arguments are "2, s0.st0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+ restore_schema_stats 
+----------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+-- No.10-2-2
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57.000002');
+NOTICE:  arguments are "3, s0.st0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+ restore_schema_stats 
+----------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+-- No.10-2-3
+SELECT dbms_stats.restore_schema_stats('s0', '2012-01-01 00:00:00');
+ restore_schema_stats 
+----------------------
+(0 rows)
+
+--#No.10-2-4 is skipped after lock tests
+--#No.10-2-5 is skipped after lock tests
+-- No.10-2-6
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+NOTICE:  arguments are "2, s0.st0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+ restore_schema_stats 
+----------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+-- No.10-2-7
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+NOTICE:  arguments are "2, s0.st0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.sft0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+NOTICE:  arguments are "2, s0.smv0, <NULL>"
+CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
+ restore_schema_stats 
+----------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+--#No.10-2-8 is skipped after lock tests
+-- No.10-2-9
+SELECT dbms_stats.restore_schema_stats('s00', '2012-02-29 23:59:57');
+ERROR:  schema "s00" not found
+-- No.10-2-10
+SELECT dbms_stats.restore_schema_stats('pg_catalog', '2012-02-29 23:59:57');
+ERROR:  restoring statistics is inhibited for system schemas: "pg_catalog"
+/*
+ * No.10-7 dbms_stats.restore_stats
+ */
+-- No.10-7-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore_stats(NULL);
+ restore_stats 
+---------------
+(0 rows)
+
+-- No.10-7-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore_stats(0);
+ERROR:  backup id 0 not found
+-- No.10-7-3
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.restore_stats(2);
+ restore_stats 
+---------------
+ pt0
+ pt0_idx
+ s0.sft0
+ s0.smv0
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st0
+ st0_idx
+ st1
+ st1_exp
+ st1_idx
+(15 rows)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats.backup_history         | AccessShareLock
+ dbms_stats.column_stats_backup    | AccessShareLock
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats.relation_stats_backup  | AccessShareLock
+ dbms_stats.relation_stats_backup  | RowShareLock
+ dbms_stats._relation_stats_locked | ExclusiveLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(8 rows)
+
+COMMIT;
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+(15 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+(17 rows)
+
+-- No.10-7-4
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(relid, relname)
+     SELECT relid::regclass, relname
+       FROM dbms_stats.relation_stats_effective;
+INSERT INTO dbms_stats._column_stats_locked(starelid, staattnum, stainherit)
+     SELECT starelid::regclass, staattnum, stainherit
+       FROM dbms_stats.column_stats_effective;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 8;
+ id | unit | comment 
+----+------+---------
+  8 | s    | (null)
+(1 row)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ st0      |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ st0      |         2 | name    | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ st1      |         1 | val     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ st1      |         2 | str     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         2 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ st1_exp  |         1 | lower   | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.sft0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         3 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s1.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s1.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(19 rows)
+
+SELECT * FROM relations_locked_v;
+   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
+------------+----------------+----------+-----------+---------------+----------
+ pt0        | public.pt0     |   (null) |    (null) |        (null) |   (null)
+ pt0_idx    | public.pt0_idx |   (null) |    (null) |        (null) |   (null)
+ st0        | public.st0     |   (null) |    (null) |        (null) |   (null)
+ st0_idx    | public.st0_idx |   (null) |    (null) |        (null) |   (null)
+ st1        | public.st1     |   (null) |    (null) |        (null) |   (null)
+ s0.st0     | s0.st0         |   (null) |    (null) |        (null) |   (null)
+ s0.st0_idx | s0.st0_idx     |   (null) |    (null) |        (null) |   (null)
+ s0.st1     | s0.st1         |   (null) |    (null) |        (null) |   (null)
+ s0.st1_idx | s0.st1_idx     |   (null) |    (null) |        (null) |   (null)
+ s0.st2     | s0.st2         |   (null) |    (null) |        (null) |   (null)
+ s0.st2_idx | s0.st2_idx     |   (null) |    (null) |        (null) |   (null)
+ st1_idx    | public.st1_idx |   (null) |    (null) |        (null) |   (null)
+ st1_exp    | public.st1_exp |   (null) |    (null) |        (null) |   (null)
+ s0.sft0    | s0.sft0        |   (null) |    (null) |        (null) |   (null)
+ s0.smv0    | s0.smv0        |   (null) |    (null) |        (null) |   (null)
+ s1.st0     | s1.st0         |   (null) |    (null) |        (null) |   (null)
+(16 rows)
+
+SELECT dbms_stats.restore_stats(8);
+ restore_stats 
+---------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+SELECT * FROM relations_locked_v;
+   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
+------------+----------------+----------+-----------+---------------+----------
+ pt0        | public.pt0     |   (null) |    (null) |        (null) |   (null)
+ pt0_idx    | public.pt0_idx |   (null) |    (null) |        (null) |   (null)
+ st0        | public.st0     |   (null) |    (null) |        (null) |   (null)
+ st0_idx    | public.st0_idx |   (null) |    (null) |        (null) |   (null)
+ st1        | public.st1     |   (null) |    (null) |        (null) |   (null)
+ s0.st0     | s0.st0         |        1 |         2 |             1 |        1
+ s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
+ s0.st1     | s0.st1         |        1 |         3 |             1 |        1
+ s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
+ s0.st2     | s0.st2         |        1 |         3 |             1 |        1
+ s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
+ st1_idx    | public.st1_idx |   (null) |    (null) |        (null) |   (null)
+ st1_exp    | public.st1_exp |   (null) |    (null) |        (null) |   (null)
+ s0.sft0    | s0.sft0        |   (null) |    (null) |        (null) |   (null)
+ s0.smv0    | s0.smv0        |   (null) |    (null) |        (null) |   (null)
+ s1.st0     | s1.st0         |   (null) |    (null) |        (null) |   (null)
+(16 rows)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ st0      |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ st0      |         2 | name    | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ st1      |         1 | val     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ st1      |         2 | str     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}          | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {40,50,60}       | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}          | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         2 | txt     | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test} | (null)     | (null)     | (null)     | (null)
+ st1_exp  |         1 | lower   | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s0.sft0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         3 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s1.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+ s1.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
+(19 rows)
+
+-- No.10-7-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 8;
+ id | unit | comment 
+----+------+---------
+  8 | s    | (null)
+(1 row)
+
+SELECT dbms_stats.restore_stats(8);
+ restore_stats 
+---------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+(6 rows)
+
+SELECT * FROM relations_locked_v;
+   relid    |  relname   | relpages | reltuples | relallvisible | curpages 
+------------+------------+----------+-----------+---------------+----------
+ s0.st0     | s0.st0     |        1 |         2 |             1 |        1
+ s0.st0_idx | s0.st0_idx |        2 |         2 |             0 |        2
+ s0.st1     | s0.st1     |        1 |         3 |             1 |        1
+ s0.st1_idx | s0.st1_idx |        2 |         3 |             0 |        2
+ s0.st2     | s0.st2     |        1 |         3 |             1 |        1
+ s0.st2_idx | s0.st2_idx |        2 |         3 |             0 |        2
+(6 rows)
+
+SELECT * FROM columns_locked_v;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}          | (null)     | (null)     | (null)     | (null)
+ s0.st1   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {40,50,60}       | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}          | (null)     | (null)     | (null)     | (null)
+ s0.st2   |         2 | txt     | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test} | (null)     | (null)     | (null)     | (null)
+(8 rows)
+
+/*
+ * No.11-1 dbms_stats.lock(relid, attname)
+ */
+-- No.11-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL, NULL);
+ERROR:  relation required
+-- No.11-1-2
+ALTER FUNCTION dbms_stats.lock(relid regclass)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(relid regclass)
+RETURNS regclass AS
+$$
+BEGIN
+       RAISE NOTICE 'arguments are %', $1;
+       RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', NULL);
+NOTICE:  arguments are s0.st0
+CONTEXT:  PL/pgSQL function dbms_stats.lock(regclass,text) line 11 at RETURN
+  lock  
+--------
+ s0.st0
+(1 row)
+
+DROP FUNCTION dbms_stats.lock(relid regclass);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
+    RENAME TO lock;
+-- No.11-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL, 'id');
+ERROR:  relation required
+-- No.11-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
+(2 rows)
+
+-- No.11-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(0, 'id');
+ERROR:  relation "-" not found
+-- No.11-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
+(2 rows)
+
+-- No.11-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_toast.pg_toast_2618', 'id');
+ERROR:  "pg_toast.pg_toast_2618" must be a table or an index
+-- No.11-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0_idx', 'id');
+ERROR:  "s0.st0_idx" must be an expression index
+-- No.11-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('st1_exp', 'lower');
+  lock   
+---------
+ st1_exp
+(1 row)
+
+SELECT * FROM relations_locked_v;
+  relid  |    relname     | relpages | reltuples | relallvisible | curpages 
+---------+----------------+----------+-----------+---------------+----------
+ st1_exp | public.st1_exp |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 |      stanumbers1       | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+------------------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ st1_exp  |         1 | lower   | f          |           0 |        5 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}    | (null)     | (null)     | (null)     | (null)
+(1 row)
+
+DELETE FROM dbms_stats._relation_stats_locked;
+-- No.11-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.ss0', 'id');
+ERROR:  "s0.ss0" must be a table or an index
+-- No.11-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sc0', 'id');
+ERROR:  "s0.sc0" must be a table or an index
+-- No.11-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sft0', 'id');
+  lock   
+---------
+ s0.sft0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+  relid  | relname | relpages | reltuples | relallvisible | curpages 
+---------+---------+----------+-----------+---------------+----------
+ s0.sft0 | s0.sft0 |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ s0.sft0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+(1 row)
+
+-- No.11-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.smv0', 'id');
+  lock   
+---------
+ s0.smv0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+  relid  | relname | relpages | reltuples | relallvisible | curpages 
+---------+---------+----------+-----------+---------------+----------
+ s0.smv0 | s0.smv0 |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0.smv0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}      | (null)     | (null)     | (null)     | (null)
+(1 row)
+
+-- No.11-1-14
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_catalog.pg_class', 'id');
+ERROR:  locking statistics is inhibited for system catalogs: "pg_class"
+-- No.11-1-15
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'dummy');
+ERROR:  column "dummy" not found in relation "s0.st0"
+-- No.11-1-16
+DELETE FROM dbms_stats._relation_stats_locked;
+DELETE FROM pg_statistic
+ WHERE starelid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0', 'id');
+ERROR:  no statistics available for column "id" of relation "s0.st0"
+VACUUM ANALYZE;
+-- No.11-1-17
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(
+    relid, relname, relpages, reltuples,
+    relallvisible,
+    curpages)
+    VALUES('s0.st0'::regclass, 's0.st0', 1, 1640,
+           1,
+           1);
+SELECT dbms_stats.lock_column_stats('s0.st0','id');
+ lock_column_stats 
+-------------------
+ s0.st0
+(1 row)
+
+UPDATE dbms_stats._column_stats_locked
+   SET (stanullfrac, stawidth, stadistinct,
+        stakind1, stakind2, stakind3, stakind4,
+        stakind5,
+        staop1, staop2, staop3, staop4,
+        staop5,
+        stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+        stanumbers5,
+        stavalues1, stavalues2, stavalues3, stavalues4
+       ,stavalues5
+       ) = (
+        NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL)
+ WHERE starelid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0', 'id');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |      1640 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
+(2 rows)
+
+-- No.11-1-18
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v
+ WHERE relid = 's0.st0'::regclass;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+(2 rows)
+
+/*
+ * No.11-2 dbms_stats.lock(relid)
+ */
+-- No.11-2-1
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.lock('s0.st0');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | AccessShareLock
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | AccessShareLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(5 rows)
+
+COMMIT;
+-- No.11-2-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL);
+ERROR:  relation required
+-- No.11-2-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('0');
+ERROR:  relation "-" not found
+-- No.11-2-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+-- No.11-2-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_toast.pg_toast_2618');
+ERROR:  locking statistics is not allowed for relations with relkind "t": "pg_toast.pg_toast_2618"
+HINT:  Only tables(r, m, f) and indexes(i) are lockable.
+-- No.11-2-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0_idx');
+    lock    
+------------
+ s0.st0_idx
+(1 row)
+
+SELECT * FROM relations_locked_v;
+   relid    |  relname   | relpages | reltuples | relallvisible | curpages 
+------------+------------+----------+-----------+---------------+----------
+ s0.st0_idx | s0.st0_idx |        2 |         2 |             0 |        2
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+-- No.11-2-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.ss0');
+ERROR:  locking statistics is not allowed for relations with relkind "S": "s0.ss0"
+HINT:  Only tables(r, m, f) and indexes(i) are lockable.
+-- No.11-2-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sc0');
+ERROR:  locking statistics is not allowed for relations with relkind "c": "s0.sc0"
+HINT:  Only tables(r, m, f) and indexes(i) are lockable.
+-- No.11-2-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sft0');
+  lock   
+---------
+ s0.sft0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+  relid  | relname | relpages | reltuples | relallvisible | curpages 
+---------+---------+----------+-----------+---------------+----------
+ s0.sft0 | s0.sft0 |        1 |        10 |             0 |        0
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ s0.sft0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+(1 row)
+
+-- No.11-2-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.smv0');
+  lock   
+---------
+ s0.smv0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+  relid  | relname | relpages | reltuples | relallvisible | curpages 
+---------+---------+----------+-----------+---------------+----------
+ s0.smv0 | s0.smv0 |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0.smv0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}      | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}    | (null)     | (null)     | (null)     | (null)
+ s0.smv0  |         3 | txt     | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,test}   | (null)     | (null)     | (null)     | (null)
+(3 rows)
+
+-- No.11-2-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_catalog.pg_class');
+ERROR:  locking statistics is not allowed for system catalogs: "pg_class"
+-- No.11-2-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_table_stats('s0.st0');
+ lock_table_stats 
+------------------
+ s0.st0
+(1 row)
+
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples,
+        relallvisible,
+        curpages)
+     = (NULL, NULL, NULL
+       ,NULL
+       )
+ WHERE relid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+-- No.11-2-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0');
+  lock  
+--------
+ s0.st0
+(1 row)
+
+SELECT * FROM relations_locked_v;
+ relid  | relname | relpages | reltuples | relallvisible | curpages 
+--------+---------+----------+-----------+---------------+----------
+ s0.st0 | s0.st0  |        1 |         2 |             1 |        1
+(1 row)
+
+SELECT * FROM columns_locked_v c;
+ starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+/*
+ * Stab function dbms_stats.lock
+ */
+ALTER FUNCTION dbms_stats.lock(relid regclass)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(relid regclass)
+RETURNS regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %', $1;
+    RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+ALTER FUNCTION dbms_stats.lock(relid regclass, attname text)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text)
+RETURNS regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %, %', $1, $2;
+    RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+/*
+ * No.12-1 dbms_stats.lock_database_stats
+ */
+-- No.12-1-1
+SELECT dbms_stats.lock_database_stats();
+NOTICE:  arguments are pt0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are pt0_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are st0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are st0_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are st1
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st0_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st1
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st1_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st2
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.st2_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are st1_idx
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are st1_exp
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.sft0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s0.smv0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+NOTICE:  arguments are s1.st0
+CONTEXT:  SQL function "lock_database_stats" statement 1
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+/*
+ * No.12-2 dbms_stats.lock_schema_stats
+ */
+-- No.12-2-1
+SELECT dbms_stats.lock_schema_stats('s0');
+NOTICE:  arguments are s0.st0
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.st0_idx
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.st1
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.st1_idx
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.st2
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.st2_idx
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.sft0
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+NOTICE:  arguments are s0.smv0
+CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
+ lock_schema_stats 
+-------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s0.sft0
+ s0.smv0
+(8 rows)
+
+-- No.12-2-2
+SELECT dbms_stats.lock_schema_stats('s00');
+ERROR:  schema "s00" not found
+-- No.12-2-3
+SELECT dbms_stats.lock_schema_stats('pg_catalog');
+ERROR:  locking statistics is not allowed for system schemas: "pg_catalog"
+/*
+ * No.12-3 dbms_stats.lock_table_stats(regclass)
+ */
+-- No.12-3-1
+SELECT dbms_stats.lock_table_stats('s0.st0');
+NOTICE:  arguments are s0.st0
+CONTEXT:  SQL function "lock_table_stats" statement 1
+ lock_table_stats 
+------------------
+ s0.st0
+(1 row)
+
+-- No.12-3-2
+SELECT dbms_stats.lock_table_stats('st0');
+NOTICE:  arguments are st0
+CONTEXT:  SQL function "lock_table_stats" statement 1
+ lock_table_stats 
+------------------
+ st0
+(1 row)
+
+-- No.12-3-3
+SELECT dbms_stats.lock_table_stats('s00.s0');
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.lock_table_stats('s00.s0');
+                                           ^
+/*
+ * No.12-4 dbms_stats.lock_table_stats(schemaname, tablename)
+ */
+-- No.12-4-1
+SELECT dbms_stats.lock_table_stats('s0', 'st0');
+NOTICE:  arguments are s0.st0
+CONTEXT:  SQL function "lock_table_stats" statement 1
+ lock_table_stats 
+------------------
+ s0.st0
+(1 row)
+
+/*
+ * No.12-5 dbms_stats.lock_column_stats(regclass, attname)
+ */
+-- No.12-5-1
+SELECT dbms_stats.lock_column_stats('s0.st0', 'id');
+NOTICE:  arguments are s0.st0, id
+CONTEXT:  SQL function "lock_column_stats" statement 1
+ lock_column_stats 
+-------------------
+ s0.st0
+(1 row)
+
+-- No.12-5-2
+SELECT dbms_stats.lock_column_stats('st0', 'id');
+NOTICE:  arguments are st0, id
+CONTEXT:  SQL function "lock_column_stats" statement 1
+ lock_column_stats 
+-------------------
+ st0
+(1 row)
+
+-- No.12-5-3
+SELECT dbms_stats.lock_column_stats('s00.s0', 'id');
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.lock_column_stats('s00.s0', 'id');
+                                            ^
+/*
+ * No.12-6 dbms_stats.lock_column_stats(schemaname, tablename, int2)
+ */
+-- No.12-6-1
+SELECT dbms_stats.lock_column_stats('s0', 'st0', 'id');
+NOTICE:  arguments are s0.st0, id
+CONTEXT:  SQL function "lock_column_stats" statement 1
+ lock_column_stats 
+-------------------
+ s0.st0
+(1 row)
+
+/*
+ * Delete Stab function lock
+ */
+DROP FUNCTION dbms_stats.lock(relid regclass);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
+    RENAME TO lock;
+DROP FUNCTION dbms_stats.lock(relid regclass, attname text);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass, attname text)
+    RENAME TO lock;
+/*
+ * No.13-1 dbms_stats.unlock
+ */
+-- No.13-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT dbms_stats.unlock();
+   unlock   
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.13-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT dbms_stats.unlock();
+   unlock   
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.13-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT dbms_stats.unlock();
+   unlock   
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.13-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.unlock();
+ unlock 
+--------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.13-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock('s0.st0');
+ unlock 
+--------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+-- No.13-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock('st0');
+ unlock 
+--------
+ st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+-- No.13-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+    19
+(1 row)
+
+SELECT dbms_stats.unlock('s00.s0');
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.unlock('s00.s0');
+                                 ^
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+    19
+(1 row)
+
+-- No.13-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock('s0.st0', 'id');
+ unlock 
+--------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+-- No.13-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock('s0.st0', 'dummy');
+ERROR:  column "dummy" not found in relation "s0.st0"
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+-- No.13-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT dbms_stats.unlock('s0.st0', 'id');
+ unlock 
+--------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+(0 rows)
+
+-- No.13-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st0      |         2
+ st1      |         1
+ st1      |         2
+ s0.st0   |         1
+ s0.st0   |         2
+ s0.st1   |         1
+ s0.st1   |         2
+ s0.st2   |         1
+ s0.st2   |         2
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+ s0.smv0  |         2
+ s0.smv0  |         3
+ s1.st0   |         1
+ s1.st0   |         2
+(17 rows)
+
+SELECT dbms_stats.unlock(NULL, 'id');
+ERROR:  relation required
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st0      |         2
+ st1      |         1
+ st1      |         2
+ s0.st0   |         1
+ s0.st0   |         2
+ s0.st1   |         1
+ s0.st1   |         2
+ s0.st2   |         1
+ s0.st2   |         2
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+ s0.smv0  |         2
+ s0.smv0  |         3
+ s1.st0   |         1
+ s1.st0   |         2
+(17 rows)
+
+-- No.13-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st0      |         2
+ st1      |         1
+ st1      |         2
+ s0.st0   |         1
+ s0.st0   |         2
+ s0.st1   |         1
+ s0.st1   |         2
+ s0.st2   |         1
+ s0.st2   |         2
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+ s0.smv0  |         2
+ s0.smv0  |         3
+ s1.st0   |         1
+ s1.st0   |         2
+(17 rows)
+
+SELECT dbms_stats.unlock('s0.st0', NULL);
+ unlock 
+--------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+ starelid | staattnum 
+----------+-----------
+ st0      |         1
+ st0      |         2
+ st1      |         1
+ st1      |         2
+ s0.st1   |         1
+ s0.st1   |         2
+ s0.st2   |         1
+ s0.st2   |         2
+ st1_exp  |         1
+ s0.sft0  |         1
+ s0.smv0  |         1
+ s0.smv0  |         2
+ s0.smv0  |         3
+ s1.st0   |         1
+ s1.st0   |         2
+(15 rows)
+
+-- No.13-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+    19
+(1 row)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock();
+   unlock   
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | AccessShareLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(4 rows)
+
+COMMIT;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+/*
+ * No.14-1 dbms_stats.unlock_database_stats
+ */
+-- No.14-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+    19
+(1 row)
+
+SELECT dbms_stats.unlock_database_stats();
+ unlock_database_stats 
+-----------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT dbms_stats.unlock_database_stats();
+ unlock_database_stats 
+-----------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.14-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.unlock_database_stats();
+ unlock_database_stats 
+-----------------------
+(0 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+-- No.14-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+    16
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+    19
+(1 row)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_database_stats();
+ unlock_database_stats 
+-----------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | AccessShareLock
+ dbms_stats._relation_stats_locked | ExclusiveLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+(4 rows)
+
+COMMIT;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+/*
+ * No.14-2 dbms_stats.unlock_schema_stats
+ */
+-- No.14-2-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s0');
+ unlock_schema_stats 
+---------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s0.sft0
+ s0.smv0
+(8 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+  relid  
+---------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ st1_idx
+ st1_exp
+ s1.st0
+(8 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ st1_exp  |     1
+ s1.st0   |     2
+(4 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-2-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s0');
+ unlock_schema_stats 
+---------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s0.sft0
+ s0.smv0
+(8 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+  relid  
+---------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ st1_idx
+ st1_exp
+ s1.st0
+(8 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-2-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s0');
+ unlock_schema_stats 
+---------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-2-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s0');
+ unlock_schema_stats 
+---------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s0.sft0
+ s0.smv0
+(8 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+  relid  
+---------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ st1_idx
+ st1_exp
+ s1.st0
+(8 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ st1_exp  |     1
+ s1.st0   |     2
+(4 rows)
+
+-- No.14-2-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s00');
+ERROR:  schema "s00" not found
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-2-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_schema_stats('pg_catalog');
+ERROR:  unlocking statistics is not allowed for system schemas: "pg_catalog"
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-2-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_schema_stats(NULL);
+ unlock_schema_stats 
+---------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-2-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_schema_stats('s0');
+ unlock_schema_stats 
+---------------------
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ s0.sft0
+ s0.smv0
+(8 rows)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(3 rows)
+
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+  relid  
+---------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ st1_idx
+ st1_exp
+ s1.st0
+(8 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ st1_exp  |     1
+ s1.st0   |     2
+(4 rows)
+
+/*
+ * No.14-3 dbms_stats.unlock_table_stats(regclass)
+ */
+-- No.14-3-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-3-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-3-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+ unlock_table_stats 
+--------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-3-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+-- No.14-3-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('st0');
+ unlock_table_stats 
+--------------------
+ st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+-- No.14-3-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s00.s0');
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.unlock_table_stats('s00.s0');
+                                             ^
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-3-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats(NULL);
+ unlock_table_stats 
+--------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-3-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+(2 rows)
+
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+/*
+ * No.14-4 dbms_stats.unlock_table_stats(schemaname, tablename)
+ */
+-- No.14-4-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0','st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-4-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-4-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+ unlock_table_stats 
+--------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+ relid 
+-------
+(0 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+(0 rows)
+
+-- No.14-4-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+-- No.14-4-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s00', 's0');
+ERROR:  schema "s00" does not exist
+CONTEXT:  SQL function "unlock_table_stats" statement 1
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-4-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats(NULL, 'st0');
+ unlock_table_stats 
+--------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-4-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0', NULL);
+ unlock_table_stats 
+--------------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+-- No.14-4-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st0   |     4
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(9 rows)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+ unlock_table_stats 
+--------------------
+ s0.st0
+(1 row)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowExclusiveLock
+(2 rows)
+
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(15 rows)
+
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+ starelid | count 
+----------+-------
+ st0      |     2
+ st1      |     2
+ s0.st1   |     2
+ s0.st2   |     2
+ st1_exp  |     1
+ s0.sft0  |     1
+ s0.smv0  |     3
+ s1.st0   |     2
+(8 rows)
+
+/*
+ * No.14-5 dbms_stats.unlock_column_stats(regclass, attname)
+ */
+-- No.14-5-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-5-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+ count 
+-------
+     0
+(1 row)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('st0', 'id');
+ unlock_column_stats 
+---------------------
+ st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(18 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'dummy');
+ERROR:  column "dummy" not found in relation "s0.st0"
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s00.s0', 'id');
+ERROR:  schema "s00" does not exist
+LINE 1: SELECT dbms_stats.unlock_column_stats('s00.s0', 'id');
+                                              ^
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats(NULL, 'id');
+ unlock_column_stats 
+---------------------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', NULL);
+ unlock_column_stats 
+---------------------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-5-9
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(2 rows)
+
+COMMIT;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+/*
+ * No.14-6 dbms_stats.unlock_column_stats(schemaname, tablename, attname)
+ */
+-- No.14-6-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+ id |                time                 | unit | comment 
+----+-------------------------------------+------+---------
+  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
+  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
+  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
+  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
+  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
+  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
+  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
+  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
+(8 rows)
+
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+ count 
+-------
+    26
+(1 row)
+
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+ count 
+-------
+    39
+(1 row)
+
+-- No.14-6-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+(0 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'dummy');
+ERROR:  column "dummy" not found in relation "s0.st0"
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats(NULL, 'st0', 'id');
+ unlock_column_stats 
+---------------------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', NULL, 'id');
+ unlock_column_stats 
+---------------------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', NULL);
+ unlock_column_stats 
+---------------------
+(0 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+-- No.14-6-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | id      | f
+ s0.st0   | id      | t
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(19 rows)
+
+BEGIN;
+SELECT * FROM internal_locks;
+ relation | mode 
+----------+------
+(0 rows)
+
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+ unlock_column_stats 
+---------------------
+ s0.st0
+(1 row)
+
+SELECT * FROM internal_locks;
+             relation              |       mode       
+-----------------------------------+------------------
+ dbms_stats._column_stats_locked   | RowExclusiveLock
+ dbms_stats._relation_stats_locked | RowShareLock
+(2 rows)
+
+COMMIT;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+ starelid | attname | stainherit 
+----------+---------+------------
+ st0      | id      | f
+ st0      | name    | f
+ st1      | val     | f
+ st1      | str     | f
+ s0.st0   | num     | f
+ s0.st0   | num     | t
+ s0.st1   | id      | f
+ s0.st1   | num     | f
+ s0.st2   | id      | f
+ s0.st2   | txt     | f
+ st1_exp  | lower   | f
+ s0.sft0  | id      | f
+ s0.smv0  | id      | f
+ s0.smv0  | num     | f
+ s0.smv0  | txt     | f
+ s1.st0   | id      | f
+ s1.st0   | num     | f
+(17 rows)
+
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+   relid    
+------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+(16 rows)
+
diff --git a/ext_scripts/pg_dbms_stats--1.0--1.3.2-9.4.sql b/ext_scripts/pg_dbms_stats--1.0--1.3.2-9.4.sql
new file mode 100644 (file)
index 0000000..88cfe3d
--- /dev/null
@@ -0,0 +1,4 @@
+/* pg_dbms_stats/pg_dbms_stats--1.0--1.3.2.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.2'" to load this file. \quit
diff --git a/ext_scripts/pg_dbms_stats--1.3.2--1.3.3-9.4.sql b/ext_scripts/pg_dbms_stats--1.3.2--1.3.3-9.4.sql
new file mode 100644 (file)
index 0000000..fa32d51
--- /dev/null
@@ -0,0 +1,1039 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.2--1.3.3.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.3'" to load this file. \quit
+
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    backup_id int8,
+    relid regclass,
+    attnum int2
+) RETURNS int8 AS
+$$
+/* Lock the backup id */
+SELECT * from dbms_stats.backup_history
+    WHERE  id = $1 FOR UPDATE;
+
+INSERT INTO dbms_stats.relation_stats_backup
+    SELECT $1, v.relid, v.relname, v.relpages, v.reltuples, v.relallvisible,
+           v.curpages, v.last_analyze, v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           dbms_stats.relation_stats_effective v
+     WHERE c.oid = v.relid
+       AND dbms_stats.is_target_relkind(relkind)
+       AND NOT dbms_stats.is_system_catalog(v.relid)
+       AND (v.relid = $2 OR $2 IS NULL);
+
+INSERT INTO dbms_stats.column_stats_backup
+    SELECT $1, atttypid, s.*
+      FROM pg_catalog.pg_class c,
+           dbms_stats.column_stats_effective s,
+           pg_catalog.pg_attribute a
+     WHERE c.oid = starelid
+       AND starelid = attrelid
+       AND staattnum = attnum
+       AND dbms_stats.is_target_relkind(relkind)
+       AND NOT dbms_stats.is_system_catalog(c.oid)
+       AND ($2 IS NULL OR starelid = $2)
+       AND ($3 IS NULL OR staattnum = $3);
+
+SELECT $1;
+$$
+LANGUAGE sql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL,
+    comment text DEFAULT NULL
+) RETURNS int8 AS
+$$
+DECLARE
+    backup_id       int8;
+    backup_relkind  "char";
+    set_attnum      int2;
+    unit_type       char;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $1 IS NOT NULL THEN
+        SELECT relkind INTO backup_relkind
+          FROM pg_catalog.pg_class WHERE oid = $1 FOR SHARE;
+        IF NOT FOUND THEN
+            RAISE EXCEPTION 'relation "%" not found', $1;
+        END IF;
+        IF NOT dbms_stats.is_target_relkind(backup_relkind) THEN
+            RAISE EXCEPTION 'relation of relkind "%" cannot have statistics to backup: "%"',
+                               backup_relkind, $1
+                               USING HINT = 'Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.';
+        END IF;
+        IF dbms_stats.is_system_catalog($1) THEN
+            RAISE EXCEPTION 'backing up statistics is inhibited for system catalogs: "%"', $1;
+        END IF;
+        IF $2 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $1 AND a.attname = $2 FOR SHARE;
+            IF set_attnum IS NULL THEN
+                RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_effective WHERE starelid = $1 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1;
+            END IF;
+            unit_type = 'c';
+        ELSE
+            unit_type = 't';
+        END IF;
+    ELSE
+        unit_type = 'd';
+    END IF;
+
+    INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum) INTO backup_id;
+    RETURN backup_id;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.backup_schema_stats(
+    schemaname text,
+    comment text
+) RETURNS int8 AS
+$$
+DECLARE
+    backup_id       int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1 FOR SHARE)
+    THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'backing up statistics is inhibited for system schemas: "%"', $1;
+    END IF;
+
+    INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, 's', comment)
+        RETURNING id INTO backup_id;
+
+    PERFORM dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn;
+
+    RETURN backup_id;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats._relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats._relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats._column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats._column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_database_stats(
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore(m.id, m.relid)
+  FROM (SELECT max(id) AS id, relid
+        FROM (SELECT r.id, r.relid
+              FROM pg_class c, dbms_stats.relation_stats_backup r,
+                   dbms_stats.backup_history b
+              WHERE c.oid = r.relid
+                AND r.id = b.id
+                AND b.time <= $1
+              FOR SHARE) t1
+        GROUP BY t1.relid
+        ORDER BY t1.relid) m;
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_schema_stats(
+    schemaname text,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'restoring statistics is inhibited for system schemas: "%"', $1;
+    END IF;
+
+    RETURN QUERY
+        SELECT dbms_stats.restore(m.id, m.relid)
+          FROM (SELECT max(id) AS id, relid
+                FROM (SELECT r.id, r.relid
+                      FROM pg_class c, pg_namespace n,
+                           dbms_stats.relation_stats_backup r,
+                           dbms_stats.backup_history b
+                      WHERE c.oid = r.relid
+                        AND c.relnamespace = n.oid
+                        AND n.nspname = $1
+                        AND r.id = b.id
+                        AND b.time <= $2
+                                       FOR SHARE) t1
+                GROUP BY t1.relid
+                ORDER BY t1.relid) m;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_stats(
+    backup_id int8
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_relid   regclass;
+    restore_attnum  int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history WHERE id = $1) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+
+    /* Lock the backup */
+    PERFORM * from dbms_stats.relation_stats_backup b
+        WHERE  id = $1 FOR SHARE;
+
+       /* Locking only _relation_stats_locked is sufficient */
+    LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR restore_relid IN
+        SELECT b.relid
+          FROM pg_class c
+          JOIN dbms_stats.relation_stats_backup b ON (c.oid = b.relid)
+         WHERE b.id = $1
+         ORDER BY c.oid::regclass::text
+    LOOP
+        UPDATE dbms_stats._relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = $1
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = $1
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_relid, restore_attnum, restore_type, cur_type  IN
+        SELECT c.oid, a.attnum, b.statypid, a.atttypid
+          FROM pg_class c
+          JOIN dbms_stats.column_stats_backup b ON (c.oid = b.starelid)
+          JOIN pg_attribute a ON (b.starelid = attrelid
+                              AND b.staattnum = a.attnum)
+         WHERE b.id = $1
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT attname INTO restore_attname
+              FROM pg_catalog.pg_attribute
+             WHERE attrelid = restore_relid
+               AND attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats._column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats._column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = $1
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    set_attnum   int2;
+    r            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $2 IS NULL THEN
+        RETURN dbms_stats.lock($1);
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION '"%" must be a table or an index', $1;
+    END IF;
+    IF EXISTS(SELECT * FROM pg_catalog.pg_index WHERE lock_relkind = 'i' AND indexrelid = $1 AND indexprs IS NULL) THEN
+        RAISE EXCEPTION '"%" must be an expression index', $1;
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is inhibited for system catalogs: "%"', $1;
+    END IF;
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /*
+        * If we don't have per-table statistics, create new one which has NULL for
+        * every statistic value for column_stats_effective.
+        */
+    IF NOT EXISTS(SELECT * FROM dbms_stats._relation_stats_locked ru
+                   WHERE ru.relid = $1 FOR SHARE) THEN
+        INSERT INTO dbms_stats._relation_stats_locked
+            SELECT $1, dbms_stats.relname(nspname, relname),
+                   NULL, NULL, NULL, NULL, NULL
+              FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
+             WHERE c.relnamespace = n.oid
+               AND c.oid = $1;
+    END IF;
+
+       /*
+        * Process for per-column statistics
+        */
+    FOR r IN
+        SELECT stainherit, stanullfrac, stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+           AND staattnum = set_attnum
+    LOOP
+        UPDATE dbms_stats._column_stats_locked c
+           SET stanullfrac = r.stanullfrac,
+               stawidth = r.stawidth,
+               stadistinct = r.stadistinct,
+               stakind1 = r.stakind1,
+               stakind2 = r.stakind2,
+               stakind3 = r.stakind3,
+               stakind4 = r.stakind4,
+               stakind5 = r.stakind5,
+               staop1 = r.staop1,
+               staop2 = r.staop2,
+               staop3 = r.staop3,
+               staop4 = r.staop4,
+               staop5 = r.staop5,
+               stanumbers1 = r.stanumbers1,
+               stanumbers2 = r.stanumbers2,
+               stanumbers3 = r.stanumbers3,
+               stanumbers4 = r.stanumbers4,
+               stanumbers5 = r.stanumbers5,
+               stavalues1 = r.stavalues1,
+               stavalues2 = r.stavalues2,
+               stavalues3 = r.stavalues3,
+               stavalues4 = r.stavalues4,
+               stavalues5 = r.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = set_attnum
+           AND c.stainherit = r.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._column_stats_locked
+                 VALUES ($1,
+                         set_attnum,
+                         r.stainherit,
+                         r.stanullfrac,
+                         r.stawidth,
+                         r.stadistinct,
+                         r.stakind1,
+                         r.stakind2,
+                         r.stakind3,
+                         r.stakind4,
+                         r.stakind5,
+                         r.staop1,
+                         r.staop2,
+                         r.staop3,
+                         r.staop4,
+                         r.staop5,
+                         r.stanumbers1,
+                         r.stanumbers2,
+                         r.stanumbers3,
+                         r.stanumbers4,
+                         r.stanumbers5,
+                         r.stavalues1,
+                         r.stavalues2,
+                         r.stavalues3,
+                         r.stavalues4,
+                         r.stavalues5);
+        END IF;
+        END LOOP;
+
+               /* If we don't have statistics at all, raise error. */
+        IF NOT FOUND THEN
+                       RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1::regclass;
+               END IF;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r, m, f) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats._relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           relallvisible = v.relallvisible,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats._relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.relallvisible, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats._column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               stakind5 = i.stakind5,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               staop5 = i.staop5,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stanumbers5 = i.stanumbers5,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4,
+               stavalues5 = i.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.stakind5,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.staop5,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stanumbers5,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4,
+                         i.stavalues5);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    RETURN QUERY
+        SELECT dbms_stats.lock(cn.oid)
+          FROM (SELECT c.oid
+                  FROM pg_class c, pg_namespace n
+                 WHERE c.relnamespace = n.oid
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND n.nspname = $1
+                 ORDER BY c.oid
+               ) cn;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats._relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats._column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats._relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats._relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats._relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats._relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats._relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.purge_stats(
+    backup_id int8,
+    force bool DEFAULT false
+) RETURNS SETOF dbms_stats.backup_history AS
+$$
+DECLARE
+    delete_id int8;
+    todelete   dbms_stats.backup_history;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL THEN
+        RAISE EXCEPTION 'NULL is not allowed as the second parameter';
+    END IF;
+
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                  WHERE id = $1 FOR UPDATE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF NOT $2 AND NOT EXISTS(SELECT *
+                               FROM dbms_stats.backup_history
+                              WHERE unit = 'd'
+                                AND id > $1) THEN
+        RAISE WARNING 'no database-wide backup will remain after purge'
+                       USING HINT = 'Give true for second parameter to purge forcibly.';
+        RETURN;
+    END IF;
+
+    FOR todelete IN
+        SELECT * FROM dbms_stats.backup_history
+         WHERE id <= $1
+         ORDER BY id FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.backup_history
+         WHERE id = todelete.id;
+        RETURN NEXT todelete;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats._column_stats_locked v
+                 JOIN dbms_stats._relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats._column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats._relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
diff --git a/ext_scripts/pg_dbms_stats--1.3.3--1.3.4-9.4.sql b/ext_scripts/pg_dbms_stats--1.3.3--1.3.4-9.4.sql
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.1.sql b/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.1.sql
new file mode 100644 (file)
index 0000000..959852c
--- /dev/null
@@ -0,0 +1,792 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.4--1.3.5.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.5'" to load this file. \quit
+
+/* Dropping unnecessary views and rename tables  */
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.relation_stats_locked;
+DROP VIEW dbms_stats.relation_stats_locked;
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.column_stats_locked;
+DROP VIEW dbms_stats.column_stats_locked;
+
+ALTER TABLE dbms_stats._relation_stats_locked
+         RENAME TO relation_stats_locked;
+ALTER TABLE dbms_stats._column_stats_locked
+         RENAME TO column_stats_locked;
+
+ALTER INDEX dbms_stats._relation_stats_locked_pkey
+         RENAME TO relation_stats_locked_pkey;
+ALTER INDEX dbms_stats._column_stats_locked_pkey
+         RENAME TO column_stats_locked_pkey;
+-- ALTER TABLE RENAME CONSTRAINT is since 9.2
+-- ALTER TABLE dbms_stats.column_stats_locked
+--       RENAME CONSTRAINT _column_stats_locked_starelid_fkey
+--                      TO column_stats_locked_starelid_fkey;
+UPDATE pg_constraint
+          SET conname = 'column_stats_locked_starelid_fkey'
+          WHERE oid =
+                        (SELECT cn.oid
+                         FROM pg_constraint cn
+                         JOIN pg_namespace n ON (n.nspname = 'dbms_stats' AND n.oid = cn.connamespace)
+                         WHERE cn.conname = '_column_stats_locked_starelid_fkey');
+
+/* Change function and view defenitions */
+CREATE OR REPLACE FUNCTION dbms_stats.merge(
+    lhs dbms_stats.column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS dbms_stats.column_stats_locked AS
+'MODULE_PATHNAME', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+CREATE OR REPLACE VIEW dbms_stats.relation_stats_effective AS
+    SELECT
+        c.oid AS relid,
+        dbms_stats.relname(nspname, c.relname) AS relname,
+        COALESCE(v.relpages, c.relpages) AS relpages,
+        COALESCE(v.reltuples, c.reltuples) AS reltuples,
+        COALESCE(v.curpages,
+            (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4)
+            AS curpages,
+        COALESCE(v.last_analyze,
+            pg_catalog.pg_stat_get_last_analyze_time(c.oid))
+            AS last_analyze,
+        COALESCE(v.last_autoanalyze,
+            pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid))
+            AS last_autoanalyze
+      FROM pg_catalog.pg_class c
+      JOIN pg_catalog.pg_namespace n
+        ON c.relnamespace = n.oid
+      LEFT JOIN dbms_stats.relation_stats_locked v
+        ON v.relid = c.oid
+     WHERE dbms_stats.is_target_relkind(c.relkind)
+       AND NOT dbms_stats.is_system_schema(nspname);
+
+CREATE OR REPLACE VIEW dbms_stats.column_stats_effective AS
+    SELECT * FROM (
+        SELECT (dbms_stats.merge(v, s)).*
+          FROM pg_catalog.pg_statistic s
+          FULL JOIN dbms_stats.column_stats_locked v
+         USING (starelid, staattnum, stainherit)
+         WHERE NOT dbms_stats.is_system_catalog(starelid)
+                  AND EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = starelid
+                          AND a.attnum = staattnum
+                          AND a.attisdropped  = false
+                       )
+        ) m
+     WHERE starelid IS NOT NULL;
+
+CREATE OR REPLACE   FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats.relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4,
+                       staop1, staop2, staop3, staop4,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+                       stavalues1, stavalues2, stavalues3, stavalues4
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_stats(
+    backup_id int8
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_relid   regclass;
+    restore_attnum  int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history WHERE id = $1) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+
+    /* Lock the backup */
+    PERFORM * from dbms_stats.relation_stats_backup b
+        WHERE  id = $1 FOR SHARE;
+
+       /* Locking only relation_stats_locked is sufficient */
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR restore_relid IN
+        SELECT b.relid
+          FROM pg_class c
+          JOIN dbms_stats.relation_stats_backup b ON (c.oid = b.relid)
+         WHERE b.id = $1
+         ORDER BY c.oid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = $1
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = $1
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_relid, restore_attnum, restore_type, cur_type  IN
+        SELECT c.oid, a.attnum, b.statypid, a.atttypid
+          FROM pg_class c
+          JOIN dbms_stats.column_stats_backup b ON (c.oid = b.starelid)
+          JOIN pg_attribute a ON (b.starelid = attrelid
+                              AND b.staattnum = a.attnum)
+         WHERE b.id = $1
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT attname INTO restore_attname
+              FROM pg_catalog.pg_attribute
+             WHERE attrelid = restore_relid
+               AND attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4,
+                       staop1, staop2, staop3, staop4,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+                       stavalues1, stavalues2, stavalues3, stavalues4
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = $1
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    set_attnum   int2;
+    r            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $2 IS NULL THEN
+        RETURN dbms_stats.lock($1);
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION '"%" must be a table or an index', $1;
+    END IF;
+    IF EXISTS(SELECT * FROM pg_catalog.pg_index WHERE lock_relkind = 'i' AND indexrelid = $1 AND indexprs IS NULL) THEN
+        RAISE EXCEPTION '"%" must be an expression index', $1;
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is inhibited for system catalogs: "%"', $1;
+    END IF;
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /*
+        * If we don't have per-table statistics, create new one which has NULL for
+        * every statistic value for column_stats_effective.
+        */
+    IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_locked ru
+                   WHERE ru.relid = $1 FOR SHARE) THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+            SELECT $1, dbms_stats.relname(nspname, relname),
+                   NULL, NULL, NULL, NULL, NULL
+              FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
+             WHERE c.relnamespace = n.oid
+               AND c.oid = $1;
+    END IF;
+
+       /*
+        * Process for per-column statistics
+        */
+    FOR r IN
+        SELECT stainherit, stanullfrac, stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4,
+               staop1, staop2, staop3, staop4,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+               stavalues1, stavalues2, stavalues3, stavalues4
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+           AND staattnum = set_attnum
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = r.stanullfrac,
+               stawidth = r.stawidth,
+               stadistinct = r.stadistinct,
+               stakind1 = r.stakind1,
+               stakind2 = r.stakind2,
+               stakind3 = r.stakind3,
+               stakind4 = r.stakind4,
+               staop1 = r.staop1,
+               staop2 = r.staop2,
+               staop3 = r.staop3,
+               staop4 = r.staop4,
+               stanumbers1 = r.stanumbers1,
+               stanumbers2 = r.stanumbers2,
+               stanumbers3 = r.stanumbers3,
+               stanumbers4 = r.stanumbers4,
+               stavalues1 = r.stavalues1,
+               stavalues2 = r.stavalues2,
+               stavalues3 = r.stavalues3,
+               stavalues4 = r.stavalues4
+         WHERE c.starelid = $1
+           AND c.staattnum = set_attnum
+           AND c.stainherit = r.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         set_attnum,
+                         r.stainherit,
+                         r.stanullfrac,
+                         r.stawidth,
+                         r.stadistinct,
+                         r.stakind1,
+                         r.stakind2,
+                         r.stakind3,
+                         r.stakind4,
+                         r.staop1,
+                         r.staop2,
+                         r.staop3,
+                         r.staop4,
+                         r.stanumbers1,
+                         r.stanumbers2,
+                         r.stanumbers3,
+                         r.stanumbers4,
+                         r.stavalues1,
+                         r.stavalues2,
+                         r.stavalues3,
+                         r.stavalues4);
+        END IF;
+        END LOOP;
+
+               /* If we don't have statistics at all, raise error. */
+        IF NOT FOUND THEN
+                       RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1::regclass;
+               END IF;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats.relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4,
+               staop1, staop2, staop3, staop4,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+               stavalues1, stavalues2, stavalues3, stavalues4
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats.column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats.relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats.relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats.column_stats_locked v
+                 JOIN dbms_stats.relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats.column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats.relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
diff --git a/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.2.sql b/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.2.sql
new file mode 100644 (file)
index 0000000..e7289e8
--- /dev/null
@@ -0,0 +1,575 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.4--1.3.5.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.5'" to load this file. \quit
+
+/* Dropping unnecessary views and rename tables  */
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.relation_stats_locked;
+DROP VIEW dbms_stats.relation_stats_locked;
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.column_stats_locked;
+DROP VIEW dbms_stats.column_stats_locked;
+
+ALTER TABLE dbms_stats._relation_stats_locked
+         RENAME TO relation_stats_locked;
+ALTER TABLE dbms_stats._column_stats_locked
+         RENAME TO column_stats_locked;
+ALTER INDEX dbms_stats._relation_stats_locked_pkey
+         RENAME TO relation_stats_locked_pkey;
+ALTER INDEX dbms_stats._column_stats_locked_pkey
+         RENAME TO column_stats_locked_pkey;
+ALTER TABLE dbms_stats.column_stats_locked
+         RENAME CONSTRAINT _column_stats_locked_starelid_fkey
+                        TO column_stats_locked_starelid_fkey;
+
+/* Change function and view defenitions */
+CREATE OR REPLACE FUNCTION dbms_stats.merge(
+    lhs dbms_stats.column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS dbms_stats.column_stats_locked AS
+'MODULE_PATHNAME', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+CREATE OR REPLACE VIEW dbms_stats.relation_stats_effective AS
+    SELECT
+        c.oid AS relid,
+        dbms_stats.relname(nspname, c.relname) AS relname,
+        COALESCE(v.relpages, c.relpages) AS relpages,
+        COALESCE(v.reltuples, c.reltuples) AS reltuples,
+        COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
+        COALESCE(v.curpages,
+            (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4)
+            AS curpages,
+        COALESCE(v.last_analyze,
+            pg_catalog.pg_stat_get_last_analyze_time(c.oid))
+            AS last_analyze,
+        COALESCE(v.last_autoanalyze,
+            pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid))
+            AS last_autoanalyze
+      FROM pg_catalog.pg_class c
+      JOIN pg_catalog.pg_namespace n
+        ON c.relnamespace = n.oid
+      LEFT JOIN dbms_stats.relation_stats_locked v
+        ON v.relid = c.oid
+     WHERE dbms_stats.is_target_relkind(c.relkind)
+       AND NOT dbms_stats.is_system_schema(nspname);
+
+CREATE OR REPLACE VIEW dbms_stats.column_stats_effective AS
+    SELECT * FROM (
+        SELECT (dbms_stats.merge(v, s)).*
+          FROM pg_catalog.pg_statistic s
+          FULL JOIN dbms_stats.column_stats_locked v
+         USING (starelid, staattnum, stainherit)
+         WHERE NOT dbms_stats.is_system_catalog(starelid)
+                  AND EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = starelid
+                          AND a.attnum = staattnum
+                          AND a.attisdropped  = false
+                       )
+        ) m
+     WHERE starelid IS NOT NULL;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats.relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r, f) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats.relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           relallvisible = v.relallvisible,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.relallvisible, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               stakind5 = i.stakind5,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               staop5 = i.staop5,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stanumbers5 = i.stanumbers5,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4,
+               stavalues5 = i.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.stakind5,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.staop5,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stanumbers5,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4,
+                         i.stavalues5);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats.column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats.relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats.relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats.column_stats_locked v
+                 JOIN dbms_stats.relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats.column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats.relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
diff --git a/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.3.sql b/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.3.sql
new file mode 100644 (file)
index 0000000..745f334
--- /dev/null
@@ -0,0 +1,810 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.4--1.3.5.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.5'" to load this file. \quit
+
+/* Dropping unnecessary views and rename tables  */
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.relation_stats_locked;
+DROP VIEW dbms_stats.relation_stats_locked;
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.column_stats_locked;
+DROP VIEW dbms_stats.column_stats_locked;
+
+ALTER TABLE dbms_stats._relation_stats_locked
+         RENAME TO relation_stats_locked;
+ALTER TABLE dbms_stats._column_stats_locked
+         RENAME TO column_stats_locked;
+ALTER INDEX dbms_stats._relation_stats_locked_pkey
+         RENAME TO relation_stats_locked_pkey;
+ALTER INDEX dbms_stats._column_stats_locked_pkey
+         RENAME TO column_stats_locked_pkey;
+ALTER TABLE dbms_stats.column_stats_locked
+         RENAME CONSTRAINT _column_stats_locked_starelid_fkey
+                        TO column_stats_locked_starelid_fkey;
+
+/* Change function defenitions */
+
+CREATE OR REPLACE FUNCTION dbms_stats.merge(
+    lhs dbms_stats.column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS dbms_stats.column_stats_locked AS
+'MODULE_PATHNAME', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+CREATE OR REPLACE VIEW dbms_stats.relation_stats_effective AS
+    SELECT
+        c.oid AS relid,
+        dbms_stats.relname(nspname, c.relname) AS relname,
+        COALESCE(v.relpages, c.relpages) AS relpages,
+        COALESCE(v.reltuples, c.reltuples) AS reltuples,
+        COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
+        COALESCE(v.curpages,
+            (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4)
+            AS curpages,
+        COALESCE(v.last_analyze,
+            pg_catalog.pg_stat_get_last_analyze_time(c.oid))
+            AS last_analyze,
+        COALESCE(v.last_autoanalyze,
+            pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid))
+            AS last_autoanalyze
+      FROM pg_catalog.pg_class c
+      JOIN pg_catalog.pg_namespace n
+        ON c.relnamespace = n.oid
+      LEFT JOIN dbms_stats.relation_stats_locked v
+        ON v.relid = c.oid
+     WHERE dbms_stats.is_target_relkind(c.relkind)
+       AND NOT dbms_stats.is_system_schema(nspname);
+
+CREATE OR REPLACE VIEW dbms_stats.column_stats_effective AS
+    SELECT * FROM (
+        SELECT (dbms_stats.merge(v, s)).*
+          FROM pg_catalog.pg_statistic s
+          FULL JOIN dbms_stats.column_stats_locked v
+         USING (starelid, staattnum, stainherit)
+         WHERE NOT dbms_stats.is_system_catalog(starelid)
+                  AND EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = starelid
+                          AND a.attnum = staattnum
+                          AND a.attisdropped  = false
+                       )
+        ) m
+     WHERE starelid IS NOT NULL;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats.relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_stats(
+    backup_id int8
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_relid   regclass;
+    restore_attnum  int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history WHERE id = $1) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+
+    /* Lock the backup */
+    PERFORM * from dbms_stats.relation_stats_backup b
+        WHERE  id = $1 FOR SHARE;
+
+       /* Locking only _relation_stats_locked is sufficient */
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR restore_relid IN
+        SELECT b.relid
+          FROM pg_class c
+          JOIN dbms_stats.relation_stats_backup b ON (c.oid = b.relid)
+         WHERE b.id = $1
+         ORDER BY c.oid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = $1
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = $1
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_relid, restore_attnum, restore_type, cur_type  IN
+        SELECT c.oid, a.attnum, b.statypid, a.atttypid
+          FROM pg_class c
+          JOIN dbms_stats.column_stats_backup b ON (c.oid = b.starelid)
+          JOIN pg_attribute a ON (b.starelid = attrelid
+                              AND b.staattnum = a.attnum)
+         WHERE b.id = $1
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT attname INTO restore_attname
+              FROM pg_catalog.pg_attribute
+             WHERE attrelid = restore_relid
+               AND attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = $1
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    set_attnum   int2;
+    r            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $2 IS NULL THEN
+        RETURN dbms_stats.lock($1);
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION '"%" must be a table or an index', $1;
+    END IF;
+    IF EXISTS(SELECT * FROM pg_catalog.pg_index WHERE lock_relkind = 'i' AND indexrelid = $1 AND indexprs IS NULL) THEN
+        RAISE EXCEPTION '"%" must be an expression index', $1;
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is inhibited for system catalogs: "%"', $1;
+    END IF;
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /*
+        * If we don't have per-table statistics, create new one which has NULL for
+        * every statistic value for column_stats_effective.
+        */
+    IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_locked ru
+                   WHERE ru.relid = $1 FOR SHARE) THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+            SELECT $1, dbms_stats.relname(nspname, relname),
+                   NULL, NULL, NULL, NULL, NULL
+              FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
+             WHERE c.relnamespace = n.oid
+               AND c.oid = $1;
+    END IF;
+
+       /*
+        * Process for per-column statistics
+        */
+    FOR r IN
+        SELECT stainherit, stanullfrac, stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+           AND staattnum = set_attnum
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = r.stanullfrac,
+               stawidth = r.stawidth,
+               stadistinct = r.stadistinct,
+               stakind1 = r.stakind1,
+               stakind2 = r.stakind2,
+               stakind3 = r.stakind3,
+               stakind4 = r.stakind4,
+               stakind5 = r.stakind5,
+               staop1 = r.staop1,
+               staop2 = r.staop2,
+               staop3 = r.staop3,
+               staop4 = r.staop4,
+               staop5 = r.staop5,
+               stanumbers1 = r.stanumbers1,
+               stanumbers2 = r.stanumbers2,
+               stanumbers3 = r.stanumbers3,
+               stanumbers4 = r.stanumbers4,
+               stanumbers5 = r.stanumbers5,
+               stavalues1 = r.stavalues1,
+               stavalues2 = r.stavalues2,
+               stavalues3 = r.stavalues3,
+               stavalues4 = r.stavalues4,
+               stavalues5 = r.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = set_attnum
+           AND c.stainherit = r.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         set_attnum,
+                         r.stainherit,
+                         r.stanullfrac,
+                         r.stawidth,
+                         r.stadistinct,
+                         r.stakind1,
+                         r.stakind2,
+                         r.stakind3,
+                         r.stakind4,
+                         r.stakind5,
+                         r.staop1,
+                         r.staop2,
+                         r.staop3,
+                         r.staop4,
+                         r.staop5,
+                         r.stanumbers1,
+                         r.stanumbers2,
+                         r.stanumbers3,
+                         r.stanumbers4,
+                         r.stanumbers5,
+                         r.stavalues1,
+                         r.stavalues2,
+                         r.stavalues3,
+                         r.stavalues4,
+                         r.stavalues5);
+        END IF;
+        END LOOP;
+
+               /* If we don't have statistics at all, raise error. */
+        IF NOT FOUND THEN
+                       RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1::regclass;
+               END IF;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r, m, f) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats.relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           relallvisible = v.relallvisible,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.relallvisible, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               stakind5 = i.stakind5,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               staop5 = i.staop5,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stanumbers5 = i.stanumbers5,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4,
+               stavalues5 = i.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.stakind5,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.staop5,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stanumbers5,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4,
+                         i.stavalues5);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats.column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats.relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats.relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats.column_stats_locked v
+                 JOIN dbms_stats.relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats.column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats.relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
+
+
+REVOKE SELECT ON dbms_stats.stats FROM PUBLIC;
+REVOKE USAGE ON schema dbms_stats FROM PUBLIC;
diff --git a/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.4.sql b/ext_scripts/pg_dbms_stats--1.3.4--1.3.5-9.4.sql
new file mode 100644 (file)
index 0000000..745f334
--- /dev/null
@@ -0,0 +1,810 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.4--1.3.5.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "ALTER EXTENSION pg_dbms_stats UPDATE TO '1.3.5'" to load this file. \quit
+
+/* Dropping unnecessary views and rename tables  */
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.relation_stats_locked;
+DROP VIEW dbms_stats.relation_stats_locked;
+ALTER EXTENSION pg_dbms_stats DROP VIEW dbms_stats.column_stats_locked;
+DROP VIEW dbms_stats.column_stats_locked;
+
+ALTER TABLE dbms_stats._relation_stats_locked
+         RENAME TO relation_stats_locked;
+ALTER TABLE dbms_stats._column_stats_locked
+         RENAME TO column_stats_locked;
+ALTER INDEX dbms_stats._relation_stats_locked_pkey
+         RENAME TO relation_stats_locked_pkey;
+ALTER INDEX dbms_stats._column_stats_locked_pkey
+         RENAME TO column_stats_locked_pkey;
+ALTER TABLE dbms_stats.column_stats_locked
+         RENAME CONSTRAINT _column_stats_locked_starelid_fkey
+                        TO column_stats_locked_starelid_fkey;
+
+/* Change function defenitions */
+
+CREATE OR REPLACE FUNCTION dbms_stats.merge(
+    lhs dbms_stats.column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS dbms_stats.column_stats_locked AS
+'MODULE_PATHNAME', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+CREATE OR REPLACE VIEW dbms_stats.relation_stats_effective AS
+    SELECT
+        c.oid AS relid,
+        dbms_stats.relname(nspname, c.relname) AS relname,
+        COALESCE(v.relpages, c.relpages) AS relpages,
+        COALESCE(v.reltuples, c.reltuples) AS reltuples,
+        COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
+        COALESCE(v.curpages,
+            (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4)
+            AS curpages,
+        COALESCE(v.last_analyze,
+            pg_catalog.pg_stat_get_last_analyze_time(c.oid))
+            AS last_analyze,
+        COALESCE(v.last_autoanalyze,
+            pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid))
+            AS last_autoanalyze
+      FROM pg_catalog.pg_class c
+      JOIN pg_catalog.pg_namespace n
+        ON c.relnamespace = n.oid
+      LEFT JOIN dbms_stats.relation_stats_locked v
+        ON v.relid = c.oid
+     WHERE dbms_stats.is_target_relkind(c.relkind)
+       AND NOT dbms_stats.is_system_schema(nspname);
+
+CREATE OR REPLACE VIEW dbms_stats.column_stats_effective AS
+    SELECT * FROM (
+        SELECT (dbms_stats.merge(v, s)).*
+          FROM pg_catalog.pg_statistic s
+          FULL JOIN dbms_stats.column_stats_locked v
+         USING (starelid, staattnum, stainherit)
+         WHERE NOT dbms_stats.is_system_catalog(starelid)
+                  AND EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = starelid
+                          AND a.attnum = staattnum
+                          AND a.attisdropped  = false
+                       )
+        ) m
+     WHERE starelid IS NOT NULL;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats.relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.restore_stats(
+    backup_id int8
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_relid   regclass;
+    restore_attnum  int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history WHERE id = $1) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+
+    /* Lock the backup */
+    PERFORM * from dbms_stats.relation_stats_backup b
+        WHERE  id = $1 FOR SHARE;
+
+       /* Locking only _relation_stats_locked is sufficient */
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR restore_relid IN
+        SELECT b.relid
+          FROM pg_class c
+          JOIN dbms_stats.relation_stats_backup b ON (c.oid = b.relid)
+         WHERE b.id = $1
+         ORDER BY c.oid::regclass::text
+    LOOP
+        UPDATE dbms_stats.relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = $1
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = $1
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_relid, restore_attnum, restore_type, cur_type  IN
+        SELECT c.oid, a.attnum, b.statypid, a.atttypid
+          FROM pg_class c
+          JOIN dbms_stats.column_stats_backup b ON (c.oid = b.starelid)
+          JOIN pg_attribute a ON (b.starelid = attrelid
+                              AND b.staattnum = a.attnum)
+         WHERE b.id = $1
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT attname INTO restore_attname
+              FROM pg_catalog.pg_attribute
+             WHERE attrelid = restore_relid
+               AND attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats.column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats.column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = $1
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    set_attnum   int2;
+    r            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $2 IS NULL THEN
+        RETURN dbms_stats.lock($1);
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION '"%" must be a table or an index', $1;
+    END IF;
+    IF EXISTS(SELECT * FROM pg_catalog.pg_index WHERE lock_relkind = 'i' AND indexrelid = $1 AND indexprs IS NULL) THEN
+        RAISE EXCEPTION '"%" must be an expression index', $1;
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is inhibited for system catalogs: "%"', $1;
+    END IF;
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /*
+        * If we don't have per-table statistics, create new one which has NULL for
+        * every statistic value for column_stats_effective.
+        */
+    IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_locked ru
+                   WHERE ru.relid = $1 FOR SHARE) THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+            SELECT $1, dbms_stats.relname(nspname, relname),
+                   NULL, NULL, NULL, NULL, NULL
+              FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
+             WHERE c.relnamespace = n.oid
+               AND c.oid = $1;
+    END IF;
+
+       /*
+        * Process for per-column statistics
+        */
+    FOR r IN
+        SELECT stainherit, stanullfrac, stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+           AND staattnum = set_attnum
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = r.stanullfrac,
+               stawidth = r.stawidth,
+               stadistinct = r.stadistinct,
+               stakind1 = r.stakind1,
+               stakind2 = r.stakind2,
+               stakind3 = r.stakind3,
+               stakind4 = r.stakind4,
+               stakind5 = r.stakind5,
+               staop1 = r.staop1,
+               staop2 = r.staop2,
+               staop3 = r.staop3,
+               staop4 = r.staop4,
+               staop5 = r.staop5,
+               stanumbers1 = r.stanumbers1,
+               stanumbers2 = r.stanumbers2,
+               stanumbers3 = r.stanumbers3,
+               stanumbers4 = r.stanumbers4,
+               stanumbers5 = r.stanumbers5,
+               stavalues1 = r.stavalues1,
+               stavalues2 = r.stavalues2,
+               stavalues3 = r.stavalues3,
+               stavalues4 = r.stavalues4,
+               stavalues5 = r.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = set_attnum
+           AND c.stainherit = r.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         set_attnum,
+                         r.stainherit,
+                         r.stanullfrac,
+                         r.stawidth,
+                         r.stadistinct,
+                         r.stakind1,
+                         r.stakind2,
+                         r.stakind3,
+                         r.stakind4,
+                         r.stakind5,
+                         r.staop1,
+                         r.staop2,
+                         r.staop3,
+                         r.staop4,
+                         r.staop5,
+                         r.stanumbers1,
+                         r.stanumbers2,
+                         r.stanumbers3,
+                         r.stanumbers4,
+                         r.stanumbers5,
+                         r.stavalues1,
+                         r.stavalues2,
+                         r.stavalues3,
+                         r.stavalues4,
+                         r.stavalues5);
+        END IF;
+        END LOOP;
+
+               /* If we don't have statistics at all, raise error. */
+        IF NOT FOUND THEN
+                       RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1::regclass;
+               END IF;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r, m, f) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats.relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           relallvisible = v.relallvisible,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats.relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.relallvisible, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats.column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               stakind5 = i.stakind5,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               staop5 = i.staop5,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stanumbers5 = i.stanumbers5,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4,
+               stavalues5 = i.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats.column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.stakind5,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.staop5,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stanumbers5,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4,
+                         i.stavalues5);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats.column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats.relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats.relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats.relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats.relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats.relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats.column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE OR REPLACE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats.column_stats_locked v
+                 JOIN dbms_stats.relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats.column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats.relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
+
+
+REVOKE SELECT ON dbms_stats.stats FROM PUBLIC;
+REVOKE USAGE ON schema dbms_stats FROM PUBLIC;
similarity index 99%
rename from ext_scripts/pg_dbms_stats--1.3.4-9.1.sql
rename to ext_scripts/pg_dbms_stats--1.3.5-9.1.sql
index b0edadc..d776832 100644 (file)
@@ -1,4 +1,4 @@
-/* pg_dbms_stats/pg_dbms_stats--1.3.4.sql */
+/* pg_dbms_stats/pg_dbms_stats--1.3.5.sql */
 
 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
 \echo Use "CREATE EXTENSION pg_dbms_stats" to load this file. \quit
similarity index 99%
rename from ext_scripts/pg_dbms_stats--1.3.4-9.2.sql
rename to ext_scripts/pg_dbms_stats--1.3.5-9.2.sql
index 43a60cf..f922358 100644 (file)
@@ -1,4 +1,4 @@
-/* pg_dbms_stats/pg_dbms_stats--1.3.4.sql */
+/* pg_dbms_stats/pg_dbms_stats--1.3.5.sql */
 
 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
 \echo Use "CREATE EXTENSION pg_dbms_stats" to load this file. \quit
similarity index 99%
rename from ext_scripts/pg_dbms_stats--1.3.4-9.3.sql
rename to ext_scripts/pg_dbms_stats--1.3.5-9.3.sql
index 0b75177..8bf1cf6 100644 (file)
@@ -1,4 +1,4 @@
-/* pg_dbms_stats/pg_dbms_stats--1.3.4.sql */
+/* pg_dbms_stats/pg_dbms_stats--1.3.5.sql */
 
 -- complain if script is sourced in psql, rather than via CREATE EXTENSION
 \echo Use "CREATE EXTENSION pg_dbms_stats" to load this file. \quit
diff --git a/ext_scripts/pg_dbms_stats--1.3.5-9.4.sql b/ext_scripts/pg_dbms_stats--1.3.5-9.4.sql
new file mode 100644 (file)
index 0000000..8bf1cf6
--- /dev/null
@@ -0,0 +1,1536 @@
+/* pg_dbms_stats/pg_dbms_stats--1.3.5.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION pg_dbms_stats" to load this file. \quit
+
+-- define alias of anyarray type because parser does not allow to use
+-- anyarray in type definitions.
+--
+CREATE FUNCTION dbms_stats.anyarray_in(cstring) RETURNS dbms_stats.anyarray
+    AS 'anyarray_in' LANGUAGE internal STRICT IMMUTABLE;
+CREATE FUNCTION dbms_stats.anyarray_out(dbms_stats.anyarray) RETURNS cstring
+    AS 'anyarray_out' LANGUAGE internal STRICT IMMUTABLE;
+CREATE FUNCTION dbms_stats.anyarray_recv(internal) RETURNS dbms_stats.anyarray
+    AS 'MODULE_PATHNAME', 'dbms_stats_array_recv' LANGUAGE C STRICT IMMUTABLE;
+CREATE FUNCTION dbms_stats.anyarray_send(dbms_stats.anyarray) RETURNS bytea
+    AS 'anyarray_send' LANGUAGE internal STRICT IMMUTABLE;
+CREATE TYPE dbms_stats.anyarray (
+    INPUT = dbms_stats.anyarray_in,
+    OUTPUT = dbms_stats.anyarray_out,
+    RECEIVE = dbms_stats.anyarray_recv,
+    SEND = dbms_stats.anyarray_send,
+    INTERNALLENGTH = VARIABLE,
+    ALIGNMENT = double,
+    STORAGE = extended,
+    CATEGORY = 'P'
+);
+
+--
+-- User defined stats tables
+--
+
+CREATE TABLE dbms_stats._relation_stats_locked (
+    relid            oid    NOT NULL,
+    relname          text   NOT NULL,
+    relpages         int4,
+    reltuples        float4,
+    relallvisible    int4,
+    curpages         int4,
+    last_analyze     timestamp with time zone,
+    last_autoanalyze timestamp with time zone,
+    PRIMARY KEY (relid)
+);
+
+CREATE TABLE dbms_stats._column_stats_locked (
+    starelid    oid    NOT NULL,
+    staattnum   int2   NOT NULL,
+    stainherit  bool   NOT NULL,
+    stanullfrac float4,
+    stawidth    int4,
+    stadistinct float4,
+    stakind1    int2,
+    stakind2    int2,
+    stakind3    int2,
+    stakind4    int2,
+    stakind5    int2,
+    staop1      oid,
+    staop2      oid,
+    staop3      oid,
+    staop4      oid,
+    staop5      oid,
+    stanumbers1 float4[],
+    stanumbers2 float4[],
+    stanumbers3 float4[],
+    stanumbers4 float4[],
+    stanumbers5 float4[],
+    stavalues1  dbms_stats.anyarray,
+    stavalues2  dbms_stats.anyarray,
+    stavalues3  dbms_stats.anyarray,
+    stavalues4  dbms_stats.anyarray,
+    stavalues5  dbms_stats.anyarray,
+    PRIMARY KEY (starelid, staattnum, stainherit),
+    FOREIGN KEY (starelid) REFERENCES dbms_stats._relation_stats_locked (relid) ON DELETE CASCADE
+);
+
+--
+-- Statistics backup tables
+--
+
+CREATE TABLE dbms_stats.backup_history (
+    id      serial8 PRIMARY KEY,
+    time    timestamp with time zone NOT NULL,
+    unit    char(1) NOT NULL,
+    comment text
+);
+
+CREATE TABLE dbms_stats.relation_stats_backup (
+    id               int8   NOT NULL,
+    relid            oid    NOT NULL,
+    relname          text   NOT NULL,
+    relpages         int4   NOT NULL,
+    reltuples        float4 NOT NULL,
+    relallvisible    int4   NOT NULL,
+    curpages         int4   NOT NULL,
+    last_analyze     timestamp with time zone,
+    last_autoanalyze timestamp with time zone,
+    PRIMARY KEY (id, relid),
+    FOREIGN KEY (id) REFERENCES dbms_stats.backup_history (id) ON DELETE CASCADE
+);
+
+CREATE TABLE dbms_stats.column_stats_backup (
+    id          int8   NOT NULL,
+    statypid    oid    NOT NULL,
+    starelid    oid    NOT NULL,
+    staattnum   int2   NOT NULL,
+    stainherit  bool   NOT NULL,
+    stanullfrac float4 NOT NULL,
+    stawidth    int4   NOT NULL,
+    stadistinct float4 NOT NULL,
+    stakind1    int2   NOT NULL,
+    stakind2    int2   NOT NULL,
+    stakind3    int2   NOT NULL,
+    stakind4    int2   NOT NULL,
+    stakind5    int2   NOT NULL,
+    staop1      oid    NOT NULL,
+    staop2      oid    NOT NULL,
+    staop3      oid    NOT NULL,
+    staop4      oid    NOT NULL,
+    staop5      oid    NOT NULL,
+    stanumbers1 float4[],
+    stanumbers2 float4[],
+    stanumbers3 float4[],
+    stanumbers4 float4[],
+    stanumbers5 float4[],
+    stavalues1  dbms_stats.anyarray,
+    stavalues2  dbms_stats.anyarray,
+    stavalues3  dbms_stats.anyarray,
+    stavalues4  dbms_stats.anyarray,
+    stavalues5  dbms_stats.anyarray,
+    PRIMARY KEY (id, starelid, staattnum, stainherit),
+    FOREIGN KEY (id) REFERENCES dbms_stats.backup_history (id) ON DELETE CASCADE,
+    FOREIGN KEY (id, starelid) REFERENCES dbms_stats.relation_stats_backup (id, relid) ON DELETE CASCADE
+);
+
+--
+-- Functions
+--
+
+CREATE FUNCTION dbms_stats.relname(nspname text, relname text)
+RETURNS text AS
+$$SELECT quote_ident($1) || '.' || quote_ident($2)$$
+LANGUAGE sql STABLE STRICT;
+
+CREATE FUNCTION dbms_stats.is_system_schema(schemaname text)
+RETURNS boolean AS
+'MODULE_PATHNAME', 'dbms_stats_is_system_schema'
+LANGUAGE C IMMUTABLE STRICT;
+
+CREATE FUNCTION dbms_stats.is_system_catalog(relid regclass)
+RETURNS boolean AS
+'MODULE_PATHNAME', 'dbms_stats_is_system_catalog'
+LANGUAGE C STABLE;
+
+CREATE FUNCTION dbms_stats.is_target_relkind(relkind "char")
+RETURNS boolean AS
+$$SELECT $1 IN ('r', 'i', 'f', 'm')$$
+LANGUAGE sql STABLE;
+
+CREATE FUNCTION dbms_stats.merge(
+    lhs dbms_stats._column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS dbms_stats._column_stats_locked AS
+'MODULE_PATHNAME', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+--
+-- Statistics views for internal use
+--    These views are used to merge authentic stats and dummy stats by hook
+--    function, so we don't grant SELECT privilege to PUBLIC.
+--
+
+CREATE VIEW dbms_stats.relation_stats_effective AS
+    SELECT
+        c.oid AS relid,
+        dbms_stats.relname(nspname, c.relname) AS relname,
+        COALESCE(v.relpages, c.relpages) AS relpages,
+        COALESCE(v.reltuples, c.reltuples) AS reltuples,
+        COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
+        COALESCE(v.curpages,
+            (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4)
+            AS curpages,
+        COALESCE(v.last_analyze,
+            pg_catalog.pg_stat_get_last_analyze_time(c.oid))
+            AS last_analyze,
+        COALESCE(v.last_autoanalyze,
+            pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid))
+            AS last_autoanalyze
+      FROM pg_catalog.pg_class c
+      JOIN pg_catalog.pg_namespace n
+        ON c.relnamespace = n.oid
+      LEFT JOIN dbms_stats._relation_stats_locked v
+        ON v.relid = c.oid
+     WHERE dbms_stats.is_target_relkind(c.relkind)
+       AND NOT dbms_stats.is_system_schema(nspname);
+
+CREATE VIEW dbms_stats.column_stats_effective AS
+    SELECT * FROM (
+        SELECT (dbms_stats.merge(v, s)).*
+          FROM pg_catalog.pg_statistic s
+          FULL JOIN dbms_stats._column_stats_locked v
+         USING (starelid, staattnum, stainherit)
+         WHERE NOT dbms_stats.is_system_catalog(starelid)
+                  AND EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = starelid
+                          AND a.attnum = staattnum
+                          AND a.attisdropped  = false
+                       )
+        ) m
+     WHERE starelid IS NOT NULL;
+
+--
+-- Statistics views for user use (including non-superusers)
+--    These views allow users to see dummy statistics about tables which the
+--    user has SELECT privilege.
+--
+
+CREATE VIEW dbms_stats.relation_stats_locked
+    AS SELECT *
+         FROM dbms_stats._relation_stats_locked;
+
+GRANT SELECT ON dbms_stats.relation_stats_locked TO PUBLIC;
+
+CREATE VIEW dbms_stats.column_stats_locked
+    AS SELECT *
+         FROM dbms_stats._column_stats_locked
+        WHERE has_column_privilege(starelid, staattnum, 'SELECT');
+
+GRANT SELECT ON dbms_stats.column_stats_locked TO PUBLIC;
+
+--
+-- Note: This view is copied from pg_stats in
+-- src/backend/catalog/system_views.sql in core source tree of version
+-- 9.3, and customized for pg_dbms_stats.  Changes from orignal one are:
+--   - rename from pg_stats to dbms_stats.stats by a view name.
+--   - changed the table name from pg_statistic to dbms_stats.column_stats_effective.
+--
+CREATE VIEW dbms_stats.stats AS
+    SELECT
+        nspname AS schemaname,
+        relname AS tablename,
+        attname AS attname,
+        stainherit AS inherited,
+        stanullfrac AS null_frac,
+        stawidth AS avg_width,
+        stadistinct AS n_distinct,
+        CASE
+            WHEN stakind1 = 1 THEN stavalues1
+            WHEN stakind2 = 1 THEN stavalues2
+            WHEN stakind3 = 1 THEN stavalues3
+            WHEN stakind4 = 1 THEN stavalues4
+            WHEN stakind5 = 1 THEN stavalues5
+        END AS most_common_vals,
+        CASE
+            WHEN stakind1 = 1 THEN stanumbers1
+            WHEN stakind2 = 1 THEN stanumbers2
+            WHEN stakind3 = 1 THEN stanumbers3
+            WHEN stakind4 = 1 THEN stanumbers4
+            WHEN stakind5 = 1 THEN stanumbers5
+        END AS most_common_freqs,
+        CASE
+            WHEN stakind1 = 2 THEN stavalues1
+            WHEN stakind2 = 2 THEN stavalues2
+            WHEN stakind3 = 2 THEN stavalues3
+            WHEN stakind4 = 2 THEN stavalues4
+            WHEN stakind5 = 2 THEN stavalues5
+        END AS histogram_bounds,
+        CASE
+            WHEN stakind1 = 3 THEN stanumbers1[1]
+            WHEN stakind2 = 3 THEN stanumbers2[1]
+            WHEN stakind3 = 3 THEN stanumbers3[1]
+            WHEN stakind4 = 3 THEN stanumbers4[1]
+            WHEN stakind5 = 3 THEN stanumbers5[1]
+        END AS correlation,
+        CASE
+            WHEN stakind1 = 4 THEN stavalues1
+            WHEN stakind2 = 4 THEN stavalues2
+            WHEN stakind3 = 4 THEN stavalues3
+            WHEN stakind4 = 4 THEN stavalues4
+            WHEN stakind5 = 4 THEN stavalues5
+        END AS most_common_elems,
+        CASE
+            WHEN stakind1 = 4 THEN stanumbers1
+            WHEN stakind2 = 4 THEN stanumbers2
+            WHEN stakind3 = 4 THEN stanumbers3
+            WHEN stakind4 = 4 THEN stanumbers4
+            WHEN stakind5 = 4 THEN stanumbers5
+        END AS most_common_elem_freqs,
+        CASE
+            WHEN stakind1 = 5 THEN stanumbers1
+            WHEN stakind2 = 5 THEN stanumbers2
+            WHEN stakind3 = 5 THEN stanumbers3
+            WHEN stakind4 = 5 THEN stanumbers4
+            WHEN stakind5 = 5 THEN stanumbers5
+        END AS elem_count_histogram
+    FROM dbms_stats.column_stats_effective s JOIN pg_class c ON (c.oid = s.starelid)
+         JOIN pg_attribute a ON (c.oid = attrelid AND attnum = s.staattnum)
+         LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
+    WHERE NOT attisdropped AND has_column_privilege(c.oid, a.attnum, 'select');
+
+GRANT SELECT ON dbms_stats.stats TO PUBLIC;
+
+--
+-- Utility functions
+--
+
+CREATE FUNCTION dbms_stats.invalidate_relation_cache()
+    RETURNS trigger AS
+    'MODULE_PATHNAME', 'dbms_stats_invalidate_relation_cache'
+    LANGUAGE C;
+
+-- Invalidate cached plans when dbms_stats._relation_stats_locked is modified.
+CREATE TRIGGER invalidate_relation_cache
+    BEFORE INSERT OR DELETE OR UPDATE
+    ON dbms_stats._relation_stats_locked
+   FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache();
+
+CREATE FUNCTION dbms_stats.invalidate_column_cache()
+    RETURNS trigger AS
+    'MODULE_PATHNAME', 'dbms_stats_invalidate_column_cache'
+    LANGUAGE C;
+
+-- Invalidate cached plans when dbms_stats._column_stats_locked is modified.
+CREATE TRIGGER invalidate_column_cache
+    BEFORE INSERT OR DELETE OR UPDATE
+    ON dbms_stats._column_stats_locked
+    FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_column_cache();
+
+--
+-- BACKUP_STATS: Statistics backup functions
+--
+
+CREATE FUNCTION dbms_stats.backup(
+    backup_id int8,
+    relid regclass,
+    attnum int2
+) RETURNS int8 AS
+$$
+/* Lock the backup id */
+SELECT * from dbms_stats.backup_history
+    WHERE  id = $1 FOR UPDATE;
+
+INSERT INTO dbms_stats.relation_stats_backup
+    SELECT $1, v.relid, v.relname, v.relpages, v.reltuples, v.relallvisible,
+           v.curpages, v.last_analyze, v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           dbms_stats.relation_stats_effective v
+     WHERE c.oid = v.relid
+       AND dbms_stats.is_target_relkind(relkind)
+       AND NOT dbms_stats.is_system_catalog(v.relid)
+       AND (v.relid = $2 OR $2 IS NULL);
+
+INSERT INTO dbms_stats.column_stats_backup
+    SELECT $1, atttypid, s.*
+      FROM pg_catalog.pg_class c,
+           dbms_stats.column_stats_effective s,
+           pg_catalog.pg_attribute a
+     WHERE c.oid = starelid
+       AND starelid = attrelid
+       AND staattnum = attnum
+       AND dbms_stats.is_target_relkind(relkind)
+       AND NOT dbms_stats.is_system_catalog(c.oid)
+       AND ($2 IS NULL OR starelid = $2)
+       AND ($3 IS NULL OR staattnum = $3);
+
+SELECT $1;
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.backup(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL,
+    comment text DEFAULT NULL
+) RETURNS int8 AS
+$$
+DECLARE
+    backup_id       int8;
+    backup_relkind  "char";
+    set_attnum      int2;
+    unit_type       char;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $1 IS NOT NULL THEN
+        SELECT relkind INTO backup_relkind
+          FROM pg_catalog.pg_class WHERE oid = $1 FOR SHARE;
+        IF NOT FOUND THEN
+            RAISE EXCEPTION 'relation "%" not found', $1;
+        END IF;
+        IF NOT dbms_stats.is_target_relkind(backup_relkind) THEN
+            RAISE EXCEPTION 'relation of relkind "%" cannot have statistics to backup: "%"',
+                               backup_relkind, $1
+                               USING HINT = 'Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.';
+        END IF;
+        IF dbms_stats.is_system_catalog($1) THEN
+            RAISE EXCEPTION 'backing up statistics is inhibited for system catalogs: "%"', $1;
+        END IF;
+        IF $2 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $1 AND a.attname = $2 FOR SHARE;
+            IF set_attnum IS NULL THEN
+                RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_effective WHERE starelid = $1 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1;
+            END IF;
+            unit_type = 'c';
+        ELSE
+            unit_type = 't';
+        END IF;
+    ELSE
+        unit_type = 'd';
+    END IF;
+
+    INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, unit_type, $3)
+        RETURNING dbms_stats.backup(id, $1, set_attnum) INTO backup_id;
+    RETURN backup_id;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.backup_database_stats(
+    comment text
+) RETURNS int8 AS
+$$
+SELECT dbms_stats.backup(NULL, NULL, $1)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.backup_schema_stats(
+    schemaname text,
+    comment text
+) RETURNS int8 AS
+$$
+DECLARE
+    backup_id       int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1 FOR SHARE)
+    THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'backing up statistics is inhibited for system schemas: "%"', $1;
+    END IF;
+
+    INSERT INTO dbms_stats.backup_history(time, unit, comment)
+        VALUES (current_timestamp, 's', comment)
+        RETURNING id INTO backup_id;
+
+    PERFORM dbms_stats.backup(backup_id, cn.oid, NULL)
+      FROM (SELECT c.oid
+              FROM pg_catalog.pg_class c,
+                   pg_catalog.pg_namespace n
+             WHERE n.nspname = schemaname
+               AND c.relnamespace = n.oid
+               AND dbms_stats.is_target_relkind(c.relkind)
+             ORDER BY c.oid
+           ) cn;
+
+    RETURN backup_id;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.backup_table_stats(
+    relid regclass,
+    comment text
+) RETURNS int8 AS
+$$
+SELECT dbms_stats.backup($1, NULL, $2)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.backup_table_stats(
+    schemaname text,
+    tablename text,
+    comment text
+) RETURNS int8 AS
+$$
+SELECT dbms_stats.backup(dbms_stats.relname($1, $2)::regclass, NULL, $3)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.backup_column_stats(
+    relid regclass,
+    attname text,
+    comment text
+) RETURNS int8 AS
+$$
+SELECT dbms_stats.backup($1, $2, $3)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.backup_column_stats(
+    schemaname text,
+    tablename text,
+    attname text,
+    comment text
+) RETURNS int8 AS
+$$
+SELECT dbms_stats.backup(dbms_stats.relname($1, $2)::regclass, $3, $4)
+$$
+LANGUAGE sql;
+
+--
+-- RESTORE_STATS: Statistics restore functions
+--
+CREATE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_id      int8;
+    restore_relid   regclass;
+    restore_attnum  int2;
+    set_attnum      int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL AND $3 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                           WHERE id <= $1 FOR SHARE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF $2 IS NOT NULL THEN
+        IF NOT EXISTS(SELECT * FROM pg_catalog.pg_class
+                               WHERE oid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'relation "%" not found', $2;
+        END IF;
+               -- Grabbing all backups for the relation which is not used in restore.
+        IF NOT EXISTS(SELECT * FROM dbms_stats.relation_stats_backup b
+                       WHERE b.id <= $1 AND b.relid = $2 FOR SHARE) THEN
+            RAISE EXCEPTION 'statistics of relation "%" not found in any backups before backup id = %', $2, $1;
+        END IF;
+        IF $3 IS NOT NULL THEN
+            SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = $2 AND a.attname = $3;
+            IF set_attnum IS NULL THEN
+                               RAISE EXCEPTION 'column "%" not found in relation %', $3, $2;
+            END IF;
+            IF NOT EXISTS(SELECT * FROM dbms_stats.column_stats_backup WHERE id <= $1 AND starelid = $2 AND staattnum = set_attnum) THEN
+                RAISE EXCEPTION 'statistics of column "%" of relation "%" are not found in any backups before',$3, $2, $1;
+            END IF;
+        END IF;
+               PERFORM * FROM dbms_stats._relation_stats_locked r
+                  WHERE r.relid = $2 FOR UPDATE;
+    ELSE
+               /* Lock the whole relation stats if relation is not specified.*/
+           LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+    END IF;
+
+    FOR restore_id, restore_relid IN
+         SELECT max(id), coid FROM
+        (SELECT b.id as id, c.oid as coid
+           FROM pg_class c, dbms_stats.relation_stats_backup b
+          WHERE (c.oid = $2 OR $2 IS NULL)
+            AND c.oid = b.relid
+            AND dbms_stats.is_target_relkind(c.relkind)
+            AND NOT dbms_stats.is_system_catalog(c.oid)
+            AND b.id <= $1
+         FOR SHARE) t
+      GROUP BY coid
+      ORDER BY coid::regclass::text
+    LOOP
+        UPDATE dbms_stats._relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = restore_id
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = restore_id
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_id, restore_relid, restore_attnum, restore_type, cur_type IN
+        SELECT t.id, t.oid, t.attnum, b.statypid, a.atttypid
+          FROM pg_attribute a,
+               dbms_stats.column_stats_backup b,
+               (SELECT max(b.id) AS id, c.oid, a.attnum
+                  FROM pg_class c, pg_attribute a, dbms_stats.column_stats_backup b
+                 WHERE (c.oid = $2 OR $2 IS NULL)
+                   AND c.oid = a.attrelid
+                   AND c.oid = b.starelid
+                   AND (a.attnum = set_attnum OR set_attnum IS NULL)
+                   AND a.attnum = b.staattnum
+                   AND NOT a.attisdropped
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND b.id <= $1
+                 GROUP BY c.oid, a.attnum) t
+         WHERE a.attrelid = t.oid
+           AND a.attnum = t.attnum
+           AND b.id = t.id
+           AND b.starelid = t.oid
+           AND b.staattnum = t.attnum
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT a.attname INTO restore_attname
+              FROM pg_catalog.pg_attribute a
+             WHERE a.attrelid = restore_relid
+               AND a.attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats._column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats._column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = restore_id
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.restore_database_stats(
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore(m.id, m.relid)
+  FROM (SELECT max(id) AS id, relid
+        FROM (SELECT r.id, r.relid
+              FROM pg_class c, dbms_stats.relation_stats_backup r,
+                   dbms_stats.backup_history b
+              WHERE c.oid = r.relid
+                AND r.id = b.id
+                AND b.time <= $1
+              FOR SHARE) t1
+        GROUP BY t1.relid
+        ORDER BY t1.relid) m;
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_schema_stats(
+    schemaname text,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'restoring statistics is inhibited for system schemas: "%"', $1;
+    END IF;
+
+    RETURN QUERY
+        SELECT dbms_stats.restore(m.id, m.relid)
+          FROM (SELECT max(id) AS id, relid
+                FROM (SELECT r.id, r.relid
+                      FROM pg_class c, pg_namespace n,
+                           dbms_stats.relation_stats_backup r,
+                           dbms_stats.backup_history b
+                      WHERE c.oid = r.relid
+                        AND c.relnamespace = n.oid
+                        AND n.nspname = $1
+                        AND r.id = b.id
+                        AND b.time <= $2
+                                       FOR SHARE) t1
+                GROUP BY t1.relid
+                ORDER BY t1.relid) m;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_table_stats(
+    relid regclass,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore(max(id), $1, NULL)
+  FROM dbms_stats.backup_history WHERE time <= $2
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_table_stats(
+    schemaname text,
+    tablename text,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore_table_stats(dbms_stats.relname($1, $2)::regclass, $3)
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_column_stats(
+    relid regclass,
+    attname text,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore(max(id), $1, $2)
+  FROM dbms_stats.backup_history WHERE time <= $3
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_column_stats(
+    schemaname text,
+    tablename text,
+    attname text,
+    as_of_timestamp timestamp with time zone
+) RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.restore(max(id), dbms_stats.relname($1, $2)::regclass, $3)
+  FROM dbms_stats.backup_history WHERE time <= $4
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.restore_stats(
+    backup_id int8
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    restore_relid   regclass;
+    restore_attnum  int2;
+    restore_attname text;
+    restore_type    regtype;
+    cur_type        regtype;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history WHERE id = $1) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+
+    /* Lock the backup */
+    PERFORM * from dbms_stats.relation_stats_backup b
+        WHERE  id = $1 FOR SHARE;
+
+       /* Locking only _relation_stats_locked is sufficient */
+    LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR restore_relid IN
+        SELECT b.relid
+          FROM pg_class c
+          JOIN dbms_stats.relation_stats_backup b ON (c.oid = b.relid)
+         WHERE b.id = $1
+         ORDER BY c.oid::regclass::text
+    LOOP
+        UPDATE dbms_stats._relation_stats_locked r
+           SET relid = b.relid,
+               relname = b.relname,
+               relpages = b.relpages,
+               reltuples = b.reltuples,
+               relallvisible = b.relallvisible,
+               curpages = b.curpages,
+               last_analyze = b.last_analyze,
+               last_autoanalyze = b.last_autoanalyze
+          FROM dbms_stats.relation_stats_backup b
+         WHERE r.relid = restore_relid
+           AND b.id = $1
+           AND b.relid = restore_relid;
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._relation_stats_locked
+            SELECT b.relid,
+                   b.relname,
+                   b.relpages,
+                   b.reltuples,
+                   b.relallvisible,
+                   b.curpages,
+                   b.last_analyze,
+                   b.last_autoanalyze
+              FROM dbms_stats.relation_stats_backup b
+             WHERE b.id = $1
+               AND b.relid = restore_relid;
+        END IF;
+        RETURN NEXT restore_relid;
+    END LOOP;
+
+    FOR restore_relid, restore_attnum, restore_type, cur_type  IN
+        SELECT c.oid, a.attnum, b.statypid, a.atttypid
+          FROM pg_class c
+          JOIN dbms_stats.column_stats_backup b ON (c.oid = b.starelid)
+          JOIN pg_attribute a ON (b.starelid = attrelid
+                              AND b.staattnum = a.attnum)
+         WHERE b.id = $1
+    LOOP
+        IF restore_type <> cur_type THEN
+            SELECT attname INTO restore_attname
+              FROM pg_catalog.pg_attribute
+             WHERE attrelid = restore_relid
+               AND attnum = restore_attnum;
+            RAISE WARNING 'data type of column "%.%" is inconsistent between database(%) and backup (%). Skip.',
+                restore_relid, restore_attname, cur_type, restore_type;
+        ELSE
+            DELETE FROM dbms_stats._column_stats_locked
+             WHERE starelid = restore_relid
+               AND staattnum = restore_attnum;
+            INSERT INTO dbms_stats._column_stats_locked
+                SELECT starelid, staattnum, stainherit,
+                       stanullfrac, stawidth, stadistinct,
+                       stakind1, stakind2, stakind3, stakind4, stakind5,
+                       staop1, staop2, staop3, staop4, staop5,
+                       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+                       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+                  FROM dbms_stats.column_stats_backup
+                 WHERE id = $1
+                   AND starelid = restore_relid
+                   AND staattnum = restore_attnum;
+        END IF;
+    END LOOP;
+
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+--
+-- LOCK_STATS: Statistics lock functions
+--
+
+CREATE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    set_attnum   int2;
+    r            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    IF $2 IS NULL THEN
+        RETURN dbms_stats.lock($1);
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION '"%" must be a table or an index', $1;
+    END IF;
+    IF EXISTS(SELECT * FROM pg_catalog.pg_index WHERE lock_relkind = 'i' AND indexrelid = $1 AND indexprs IS NULL) THEN
+        RAISE EXCEPTION '"%" must be an expression index', $1;
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is inhibited for system catalogs: "%"', $1;
+    END IF;
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /*
+        * If we don't have per-table statistics, create new one which has NULL for
+        * every statistic value for column_stats_effective.
+        */
+    IF NOT EXISTS(SELECT * FROM dbms_stats._relation_stats_locked ru
+                   WHERE ru.relid = $1 FOR SHARE) THEN
+        INSERT INTO dbms_stats._relation_stats_locked
+            SELECT $1, dbms_stats.relname(nspname, relname),
+                   NULL, NULL, NULL, NULL, NULL
+              FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n
+             WHERE c.relnamespace = n.oid
+               AND c.oid = $1;
+    END IF;
+
+       /*
+        * Process for per-column statistics
+        */
+    FOR r IN
+        SELECT stainherit, stanullfrac, stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+           AND staattnum = set_attnum
+    LOOP
+        UPDATE dbms_stats._column_stats_locked c
+           SET stanullfrac = r.stanullfrac,
+               stawidth = r.stawidth,
+               stadistinct = r.stadistinct,
+               stakind1 = r.stakind1,
+               stakind2 = r.stakind2,
+               stakind3 = r.stakind3,
+               stakind4 = r.stakind4,
+               stakind5 = r.stakind5,
+               staop1 = r.staop1,
+               staop2 = r.staop2,
+               staop3 = r.staop3,
+               staop4 = r.staop4,
+               staop5 = r.staop5,
+               stanumbers1 = r.stanumbers1,
+               stanumbers2 = r.stanumbers2,
+               stanumbers3 = r.stanumbers3,
+               stanumbers4 = r.stanumbers4,
+               stanumbers5 = r.stanumbers5,
+               stavalues1 = r.stavalues1,
+               stavalues2 = r.stavalues2,
+               stavalues3 = r.stavalues3,
+               stavalues4 = r.stavalues4,
+               stavalues5 = r.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = set_attnum
+           AND c.stainherit = r.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._column_stats_locked
+                 VALUES ($1,
+                         set_attnum,
+                         r.stainherit,
+                         r.stanullfrac,
+                         r.stawidth,
+                         r.stadistinct,
+                         r.stakind1,
+                         r.stakind2,
+                         r.stakind3,
+                         r.stakind4,
+                         r.stakind5,
+                         r.staop1,
+                         r.staop2,
+                         r.staop3,
+                         r.staop4,
+                         r.staop5,
+                         r.stanumbers1,
+                         r.stanumbers2,
+                         r.stanumbers3,
+                         r.stanumbers4,
+                         r.stanumbers5,
+                         r.stavalues1,
+                         r.stavalues2,
+                         r.stavalues3,
+                         r.stavalues4,
+                         r.stavalues5);
+        END IF;
+        END LOOP;
+
+               /* If we don't have statistics at all, raise error. */
+        IF NOT FOUND THEN
+                       RAISE EXCEPTION 'no statistics available for column "%" of relation "%"', $2, $1::regclass;
+               END IF;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock or restore operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.lock(relid regclass)
+    RETURNS regclass AS
+$$
+DECLARE
+    lock_relkind "char";
+    i            record;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+    SELECT relkind INTO lock_relkind FROM pg_catalog.pg_class WHERE oid = $1;
+    IF NOT FOUND THEN
+        RAISE EXCEPTION 'relation "%" not found', $1;
+    END IF;
+    IF NOT dbms_stats.is_target_relkind(lock_relkind) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for relations with relkind "%": "%"', lock_relkind, $1
+                       USING HINT = 'Only tables(r, m, f) and indexes(i) are lockable.';
+    END IF;
+    IF dbms_stats.is_system_catalog($1) THEN
+               RAISE EXCEPTION 'locking statistics is not allowed for system catalogs: "%"', $1;
+    END IF;
+
+    UPDATE dbms_stats._relation_stats_locked r
+       SET relname = dbms_stats.relname(nspname, c.relname),
+           relpages = v.relpages,
+           reltuples = v.reltuples,
+           relallvisible = v.relallvisible,
+           curpages = v.curpages,
+           last_analyze = v.last_analyze,
+           last_autoanalyze = v.last_autoanalyze
+      FROM pg_catalog.pg_class c,
+           pg_catalog.pg_namespace n,
+           dbms_stats.relation_stats_effective v
+     WHERE r.relid = $1
+       AND c.oid = $1
+       AND c.relnamespace = n.oid
+       AND v.relid = $1;
+    IF NOT FOUND THEN
+        INSERT INTO dbms_stats._relation_stats_locked
+        SELECT $1, dbms_stats.relname(nspname, c.relname),
+               v.relpages, v.reltuples, v.relallvisible, v.curpages,
+               v.last_analyze, v.last_autoanalyze
+          FROM pg_catalog.pg_class c,
+               pg_catalog.pg_namespace n,
+               dbms_stats.relation_stats_effective v
+         WHERE c.oid = $1
+           AND c.relnamespace = n.oid
+           AND v.relid = $1;
+    END IF;
+
+    IF EXISTS(SELECT *
+                FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_index ind
+                  ON c.oid = ind.indexrelid
+               WHERE c.oid = $1
+                 AND c.relkind = 'i'
+                 AND ind.indexprs IS NULL) THEN
+        RETURN $1;
+    END IF;
+
+    FOR i IN
+        SELECT staattnum, stainherit, stanullfrac,
+               stawidth, stadistinct,
+               stakind1, stakind2, stakind3, stakind4, stakind5,
+               staop1, staop2, staop3, staop4, staop5,
+               stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+               stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+          FROM dbms_stats.column_stats_effective
+         WHERE starelid = $1
+    LOOP
+        UPDATE dbms_stats._column_stats_locked c
+           SET stanullfrac = i.stanullfrac,
+               stawidth = i.stawidth,
+               stadistinct = i.stadistinct,
+               stakind1 = i.stakind1,
+               stakind2 = i.stakind2,
+               stakind3 = i.stakind3,
+               stakind4 = i.stakind4,
+               stakind5 = i.stakind5,
+               staop1 = i.staop1,
+               staop2 = i.staop2,
+               staop3 = i.staop3,
+               staop4 = i.staop4,
+               staop5 = i.staop5,
+               stanumbers1 = i.stanumbers1,
+               stanumbers2 = i.stanumbers2,
+               stanumbers3 = i.stanumbers3,
+               stanumbers4 = i.stanumbers4,
+               stanumbers5 = i.stanumbers5,
+               stavalues1 = i.stavalues1,
+               stavalues2 = i.stavalues2,
+               stavalues3 = i.stavalues3,
+               stavalues4 = i.stavalues4,
+               stavalues5 = i.stavalues5
+         WHERE c.starelid = $1
+           AND c.staattnum = i.staattnum
+           AND c.stainherit = i.stainherit;
+
+        IF NOT FOUND THEN
+            INSERT INTO dbms_stats._column_stats_locked
+                 VALUES ($1,
+                         i.staattnum,
+                         i.stainherit,
+                         i.stanullfrac,
+                         i.stawidth,
+                         i.stadistinct,
+                         i.stakind1,
+                         i.stakind2,
+                         i.stakind3,
+                         i.stakind4,
+                         i.stakind5,
+                         i.staop1,
+                         i.staop2,
+                         i.staop3,
+                         i.staop4,
+                         i.staop5,
+                         i.stanumbers1,
+                         i.stanumbers2,
+                         i.stanumbers3,
+                         i.stanumbers4,
+                         i.stanumbers5,
+                         i.stavalues1,
+                         i.stavalues2,
+                         i.stavalues3,
+                         i.stavalues4,
+                         i.stavalues5);
+            END IF;
+        END LOOP;
+
+    RETURN $1;
+EXCEPTION
+  WHEN unique_violation THEN
+    RAISE EXCEPTION 'This operation is canceled by simultaneous lock operation on the same relation.';
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.lock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+SELECT dbms_stats.lock(c.oid)
+  FROM (SELECT oid
+          FROM pg_catalog.pg_class
+         WHERE NOT dbms_stats.is_system_catalog(oid)
+           AND dbms_stats.is_target_relkind(relkind)
+         ORDER BY pg_class.oid
+       ) c;
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.lock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'locking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    RETURN QUERY
+        SELECT dbms_stats.lock(cn.oid)
+          FROM (SELECT c.oid
+                  FROM pg_class c, pg_namespace n
+                 WHERE c.relnamespace = n.oid
+                   AND dbms_stats.is_target_relkind(c.relkind)
+                   AND n.nspname = $1
+                 ORDER BY c.oid
+               ) cn;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE FUNCTION dbms_stats.lock_table_stats(relid regclass)
+  RETURNS regclass AS
+$$
+SELECT dbms_stats.lock($1)
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.lock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS regclass AS
+$$
+SELECT dbms_stats.lock(dbms_stats.relname($1, $2)::regclass)
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.lock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS regclass AS
+$$
+SELECT dbms_stats.lock($1, $2)
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.lock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS regclass AS
+$$
+SELECT dbms_stats.lock(dbms_stats.relname($1, $2)::regclass, $3)
+$$
+LANGUAGE sql STRICT;
+
+--
+-- UNLOCK_STATS: Statistics unlock functions
+--
+
+CREATE FUNCTION dbms_stats.unlock(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+    unlock_id  int8;
+BEGIN
+    IF $1 IS NULL AND $2 IS NOT NULL THEN
+        RAISE EXCEPTION 'relation required';
+    END IF;
+
+       /*
+        * Lock the target relation to prevent conflicting with stats lock/restore
+     */
+       PERFORM * FROM dbms_stats._relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) FOR UPDATE;
+
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+    DELETE FROM dbms_stats._column_stats_locked
+     WHERE (starelid = $1 OR $1 IS NULL)
+       AND (staattnum = set_attnum OR $2 IS NULL);
+
+    IF $1 IS NOT NULL AND $2 IS NOT NULL THEN
+        RETURN QUERY
+            SELECT $1;
+    END IF;
+    FOR unlock_id IN
+        SELECT ru.relid
+          FROM dbms_stats._relation_stats_locked ru
+         WHERE (ru.relid = $1 OR $1 IS NULL) AND ($2 IS NULL)
+         ORDER BY ru.relid
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked ru
+         WHERE ru.relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+CREATE FUNCTION dbms_stats.unlock_database_stats()
+  RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    LOCK dbms_stats._relation_stats_locked IN EXCLUSIVE MODE;
+
+    FOR unlock_id IN
+        SELECT relid
+          FROM dbms_stats._relation_stats_locked
+         ORDER BY relid
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE FUNCTION dbms_stats.unlock_schema_stats(
+    schemaname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    unlock_id int8;
+BEGIN
+    IF NOT EXISTS(SELECT * FROM pg_namespace WHERE nspname = $1) THEN
+        RAISE EXCEPTION 'schema "%" not found', $1;
+    END IF;
+    IF dbms_stats.is_system_schema($1) THEN
+        RAISE EXCEPTION 'unlocking statistics is not allowed for system schemas: "%"', $1;
+    END IF;
+
+    FOR unlock_id IN
+        SELECT r.relid
+          FROM dbms_stats._relation_stats_locked r, pg_class c, pg_namespace n
+         WHERE relid = c.oid
+           AND c.relnamespace = n.oid
+           AND n.nspname = $1
+         ORDER BY relid
+         FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats._relation_stats_locked
+         WHERE relid = unlock_id;
+        RETURN NEXT unlock_id;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE FUNCTION dbms_stats.unlock_table_stats(relid regclass)
+  RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats._relation_stats_locked
+ WHERE relid = $1
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.unlock_table_stats(
+    schemaname text,
+    tablename text
+) RETURNS SETOF regclass AS
+$$
+DELETE FROM dbms_stats._relation_stats_locked
+ WHERE relid = dbms_stats.relname($1, $2)::regclass
+ RETURNING relid::regclass
+$$
+LANGUAGE sql STRICT;
+
+CREATE FUNCTION dbms_stats.unlock_column_stats(
+    relid regclass,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = $1 AND a.attname = $2;
+    IF $2 IS NOT NULL AND set_attnum IS NULL THEN
+        RAISE EXCEPTION 'column "%" not found in relation "%"', $2, $1;
+    END IF;
+
+       /* Lock the locked table stats */
+    PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE r.relid = $1 FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = $1
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT $1;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+CREATE FUNCTION dbms_stats.unlock_column_stats(
+    schemaname text,
+    tablename text,
+    attname text
+) RETURNS SETOF regclass AS
+$$
+DECLARE
+    set_attnum int2;
+BEGIN
+    SELECT a.attnum INTO set_attnum FROM pg_catalog.pg_attribute a
+     WHERE a.attrelid = dbms_stats.relname($1, $2)::regclass
+       AND a.attname = $3;
+    IF $3 IS NOT NULL AND set_attnum IS NULL THEN
+               RAISE EXCEPTION 'column "%" not found in relation "%.%"', $3, $1, $2;
+    END IF;
+
+       /* Lock the locked table stats */
+       PERFORM * from dbms_stats.relation_stats_locked r
+        WHERE  relid = dbms_stats.relname($1, $2)::regclass FOR SHARE;
+
+    DELETE FROM dbms_stats._column_stats_locked
+      WHERE starelid = dbms_stats.relname($1, $2)::regclass
+        AND staattnum = set_attnum;
+
+    RETURN QUERY
+        SELECT dbms_stats.relname($1, $2)::regclass;
+END;
+$$
+LANGUAGE plpgsql STRICT;
+
+--
+-- IMPORT_STATS: Statistics import functions
+--
+
+CREATE FUNCTION dbms_stats.import(
+    nspname text DEFAULT NULL,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL,
+    src text DEFAULT NULL
+) RETURNS void AS
+'MODULE_PATHNAME', 'dbms_stats_import'
+LANGUAGE C;
+
+CREATE FUNCTION dbms_stats.import_database_stats(src text)
+  RETURNS void AS
+$$
+SELECT dbms_stats.import(NULL, NULL, NULL, $1)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.import_schema_stats(
+    schemaname text,
+    src text
+) RETURNS void AS
+$$
+SELECT dbms_stats.import($1, NULL, NULL, $2)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.import_table_stats(
+    relid regclass,
+    src text
+) RETURNS void AS
+$$
+SELECT dbms_stats.import(NULL, $1, NULL, $2)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.import_table_stats(
+    schemaname text,
+    tablename text,
+    src text
+) RETURNS void AS
+$$
+SELECT dbms_stats.import(NULL, dbms_stats.relname($1, $2)::regclass, NULL, $3)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.import_column_stats(
+    relid regclass,
+    attname text,
+    src text
+) RETURNS void AS
+$$
+SELECT dbms_stats.import(NULL, $1, $2, $3)
+$$
+LANGUAGE sql;
+
+CREATE FUNCTION dbms_stats.import_column_stats(
+    schemaname text,
+    tablename text,
+    attname text,
+    src text
+) RETURNS void AS
+$$
+SELECT dbms_stats.import(NULL, dbms_stats.relname($1, $2)::regclass, $3, $4)
+$$
+LANGUAGE sql;
+
+--
+-- PURGE_STATS: Statistics purge function
+--
+CREATE FUNCTION dbms_stats.purge_stats(
+    backup_id int8,
+    force bool DEFAULT false
+) RETURNS SETOF dbms_stats.backup_history AS
+$$
+DECLARE
+    delete_id int8;
+    todelete   dbms_stats.backup_history;
+BEGIN
+    IF $1 IS NULL THEN
+        RAISE EXCEPTION 'backup id required';
+    END IF;
+    IF $2 IS NULL THEN
+        RAISE EXCEPTION 'NULL is not allowed as the second parameter';
+    END IF;
+
+    IF NOT EXISTS(SELECT * FROM dbms_stats.backup_history
+                  WHERE id = $1 FOR UPDATE) THEN
+        RAISE EXCEPTION 'backup id % not found', $1;
+    END IF;
+    IF NOT $2 AND NOT EXISTS(SELECT *
+                               FROM dbms_stats.backup_history
+                              WHERE unit = 'd'
+                                AND id > $1) THEN
+        RAISE WARNING 'no database-wide backup will remain after purge'
+                       USING HINT = 'Give true for second parameter to purge forcibly.';
+        RETURN;
+    END IF;
+
+    FOR todelete IN
+        SELECT * FROM dbms_stats.backup_history
+         WHERE id <= $1
+         ORDER BY id FOR UPDATE
+    LOOP
+        DELETE FROM dbms_stats.backup_history
+         WHERE id = todelete.id;
+        RETURN NEXT todelete;
+    END LOOP;
+END;
+$$
+LANGUAGE plpgsql;
+
+--
+-- CLEAN_STATS: Clean orphan dummy statistics
+--
+CREATE FUNCTION dbms_stats.clean_up_stats() RETURNS SETOF text AS
+$$
+DECLARE
+       clean_relid             Oid;
+       clean_attnum    int2;
+       clean_inherit   bool;
+       clean_rel_col   text;
+BEGIN
+       -- We don't have to check that table-level dummy statistics of the table
+       -- exists here, because the foreign key constraints defined on column-level
+       -- dummy static table ensures that.
+       FOR clean_rel_col, clean_relid, clean_attnum, clean_inherit IN
+               SELECT r.relname || ', ' || v.staattnum::text,
+                          v.starelid, v.staattnum, v.stainherit
+                 FROM dbms_stats._column_stats_locked v
+                 JOIN dbms_stats._relation_stats_locked r ON (v.starelid = r.relid)
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_attribute a
+                        WHERE a.attrelid = v.starelid
+                          AND a.attnum = v.staattnum
+                          AND a.attisdropped  = false
+         FOR UPDATE
+               )
+       LOOP
+               DELETE FROM dbms_stats._column_stats_locked
+                WHERE starelid = clean_relid
+                  AND staattnum = clean_attnum
+                  AND stainherit = clean_inherit;
+               RETURN NEXT clean_rel_col;
+       END LOOP;
+
+       RETURN QUERY
+               DELETE FROM dbms_stats._relation_stats_locked r
+                WHERE NOT EXISTS (
+                       SELECT NULL
+                         FROM pg_class c
+                        WHERE c.oid = r.relid)
+                RETURNING relname || ',';
+       RETURN;
+END
+$$
+LANGUAGE plpgsql;
+
+GRANT USAGE ON schema dbms_stats TO PUBLIC;
+--
diff --git a/input/ut_imp_exp-9.4.source b/input/ut_imp_exp-9.4.source
new file mode 100644 (file)
index 0000000..2c81c1c
--- /dev/null
@@ -0,0 +1,293 @@
+\pset null '(null)'
+CREATE TABLE s0.st3();
+/*
+ * No.16-1 export_plain_stats-9.3.sql.sample
+ */
+-- No.16-1-1
+ANALYZE;
+DELETE FROM dbms_stats._column_stats_locked;
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples, relallvisible, curpages) = (0,0,0,0);
+UPDATE dbms_stats._column_stats_locked SET
+    stanullfrac = -staattnum,
+    stawidth = -staattnum,
+    stadistinct = -staattnum,
+    stakind1 = 2,
+    stakind2 = 3,
+    stakind3 = 4,
+    stakind4 = 1,
+    stakind5 = 5,
+    staop1 = 22,
+    staop2 = 23,
+    staop3 = 24,
+    staop4 = 21,
+    staop5 = 25,
+    stanumbers1 = ARRAY[-staattnum,22],
+    stanumbers2 = ARRAY[-staattnum,23],
+    stanumbers3 = ARRAY[-staattnum,24],
+    stanumbers4 = ARRAY[-staattnum,21],
+    stanumbers5 = ARRAY[-staattnum,25],
+    stavalues1 = stavalues3,
+    stavalues2 = stavalues2,
+    stavalues3 = stavalues1,
+    stavalues4 = stavalues4,
+    stavalues5 = stavalues5;
+\i doc/export_plain_stats-9.3.sql.sample
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+-- No.16-1-2
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-3
+\! sed '/ORDER/i\\ AND c.relname = '"\'st0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-4
+\! sed '/ORDER/i\\ AND c.relname = '"\'pg_toast_1262\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-5
+\! sed '/ORDER/i\\ AND c.relname = '"\'st0_idx\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-6
+\! sed '/ORDER/i\\ AND c.relname = '"\'ss0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-7
+\! sed '/ORDER/i\\ AND c.relname = '"\'sc0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-8
+\! sed '/ORDER/i\\ AND c.relname = '"\'sft0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-9
+\! sed '/ORDER/i\\ AND c.relname = '"\'smv0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-10
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'"' AND a.attname = '\'id\' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-11
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'"' AND a.attname IS NULL' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-12
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s1\'"' AND c.relname IS NULL' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+
+/*
+ * No.16-2 export_effective_stats-9.3.sql.sample
+ */
+-- No.16-2-1
+VACUUM ANALYZE;
+SELECT dbms_stats.lock_database_stats();
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples, relallvisible, curpages) = (NULL, NULL, NULL, NULL);
+UPDATE dbms_stats._column_stats_locked
+   SET (stanullfrac, stawidth, stadistinct,
+        stakind1, stakind2, stakind3, stakind4, stakind5,
+        staop1, staop2, staop3, staop4, staop5,
+        stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+        stavalues1, stavalues2, stavalues3, stavalues4, stavalues5)
+     = (NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL)
+ WHERE starelid = 's0.st0'::regclass;
+\i doc/export_effective_stats-9.3.sql.sample
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+TRUNCATE dbms_stats.work;
+-- No.16-2-2
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-3
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'st0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-4
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'pg_toast_1262\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-5
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'st0_idx\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-6
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'ss0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-7
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'sc0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-8
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'sft0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-9
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'smv0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-10
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND a.attname = '"\'id\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-11
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND a.attname IS NULL' doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-12
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND cl.relname IS NULL' doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+
+/*
+ * Stab function dbms_stats.import
+ */
+ALTER FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RENAME TO truth_import;
+CREATE FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RETURNS void AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are "%", "%", "%", "%"', $1, $2, $3, $4;
+    RETURN;
+END
+$$
+LANGUAGE plpgsql;
+/*
+ * No.17-1 dbms_stats.import_database_stats(src)
+ */
+-- No.17-1-1
+SELECT dbms_stats.import_database_stats('@abs_srcdir@/export_stats.dmp');
+
+/*
+ * No.17-2 dbms_stats.import_schema_stats(schemaname, src)
+ */
+-- No.17-2-1
+SELECT dbms_stats.import_schema_stats('s0', '@abs_srcdir@/export_stats.dmp');
+
+/*
+ * No.17-3 dbms_stats.import_table_stats(relid, src)
+ */
+-- No.17-3-1
+SELECT dbms_stats.import_table_stats('s0.st0', '@abs_srcdir@/export_stats.dmp');
+
+/*
+ * No.17-4 dbms_stats.import_table_stats(schemaname, tablename, src)
+ */
+-- No.17-4-1
+SELECT dbms_stats.import_table_stats('s0', 'st0', '@abs_srcdir@/export_stats.dmp');
+
+/*
+ * No.17-5 dbms_stats.import_column_stats (relid, attname, src)
+ */
+-- No.17-5-1
+SELECT dbms_stats.import_column_stats('s0.st0', 'id', '@abs_srcdir@/export_stats.dmp');
+
+/*
+ * No.17-6 dbms_stats.import_column_stats (schemaname, tablename, attname, src)
+ */
+-- No.17-6-1
+SELECT dbms_stats.import_column_stats('s0', 'st0', 'id','@abs_srcdir@/export_stats.dmp');
+
+/*
+ * Delete stab function dbms_stats.import
+ */
+DROP FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+);
+ALTER FUNCTION dbms_stats.truth_import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RENAME TO import;
diff --git a/output/ut_imp_exp-9.4.source b/output/ut_imp_exp-9.4.source
new file mode 100644 (file)
index 0000000..1cee18e
--- /dev/null
@@ -0,0 +1,2271 @@
+\pset null '(null)'
+CREATE TABLE s0.st3();
+/*
+ * No.16-1 export_plain_stats-9.3.sql.sample
+ */
+-- No.16-1-1
+ANALYZE;
+DELETE FROM dbms_stats._column_stats_locked;
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+ s0.st3
+(17 rows)
+
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples, relallvisible, curpages) = (0,0,0,0);
+UPDATE dbms_stats._column_stats_locked SET
+    stanullfrac = -staattnum,
+    stawidth = -staattnum,
+    stadistinct = -staattnum,
+    stakind1 = 2,
+    stakind2 = 3,
+    stakind3 = 4,
+    stakind4 = 1,
+    stakind5 = 5,
+    staop1 = 22,
+    staop2 = 23,
+    staop3 = 24,
+    staop4 = 21,
+    staop5 = 25,
+    stanumbers1 = ARRAY[-staattnum,22],
+    stanumbers2 = ARRAY[-staattnum,23],
+    stanumbers3 = ARRAY[-staattnum,24],
+    stanumbers4 = ARRAY[-staattnum,21],
+    stanumbers5 = ARRAY[-staattnum,25],
+    stavalues1 = stavalues3,
+    stavalues2 = stavalues2,
+    stavalues3 = stavalues1,
+    stavalues4 = stavalues4,
+    stavalues5 = stavalues5;
+\i doc/export_plain_stats-9.3.sql.sample
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 |      stanumbers1       | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+------------------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ public  | pt0     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ public  | pt0_idx |        2 |         0 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | name    | pg_catalog          | bpchar  |         9 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}                    | {1}         | (null)      | (null)      | (null)      | {"test "}              | (null)     | (null)     | (null)     | (null)
+ public  | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ public  | st1     |       45 |     10000 |            45 |       45 | str     | pg_catalog          | text    |        -1 | f          |           0 |        2 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+ public  | st1     |       45 |     10000 |            45 |       45 | val     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |           3 |        1 |        3 |        0 |        0 |        0 |     96 |     97 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+ public  | st1_exp |       30 |     10000 |             0 |       30 | lower   | pg_catalog          | text    |        -1 | f          |           0 |        5 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
+ public  | st1_idx |       30 |     10000 |             0 |       30 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,test}               | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)                 | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4}              | (null)     | (null)     | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {15,25,35,45}          | (null)     | (null)     | (null)     | (null)
+(27 rows)
+
+TRUNCATE dbms_stats.work;
+-- No.16-1-2
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND n2.nspname = 's0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,test}               | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)                 | (null)     | (null)     | (null)     | (null)
+(16 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-3
+\! sed '/ORDER/i\\ AND c.relname = '"\'st0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'st0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
+ public  | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | name    | pg_catalog          | bpchar  |         9 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}         | {1}         | (null)      | (null)      | (null)      | {"test "}        | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4}        | (null)     | (null)     | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {15,25,35,45}    | (null)     | (null)     | (null)     | (null)
+(8 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-4
+\! sed '/ORDER/i\\ AND c.relname = '"\'pg_toast_1262\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'pg_toast_1262'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-5
+\! sed '/ORDER/i\\ AND c.relname = '"\'st0_idx\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'st0_idx'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ public  | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(2 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-6
+\! sed '/ORDER/i\\ AND c.relname = '"\'ss0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'ss0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-7
+\! sed '/ORDER/i\\ AND c.relname = '"\'sc0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'sc0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-8
+\! sed '/ORDER/i\\ AND c.relname = '"\'sft0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'sft0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+(1 row)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-9
+\! sed '/ORDER/i\\ AND c.relname = '"\'smv0\'" doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND c.relname = 'smv0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}      | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}    | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,test}   | (null)     | (null)     | (null)     | (null)
+(3 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-10
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'"' AND a.attname = '\'id\' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND n2.nspname = 's0' AND a.attname = 'id'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
+(6 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-11
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s0\'"' AND a.attname IS NULL' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND n2.nspname = 's0' AND a.attname IS NULL
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+-- No.16-1-12
+\! sed '/ORDER/i\\ AND n2.nspname = '"\'s1\'"' AND c.relname IS NULL' doc/export_plain_stats-9.3.sql.sample > doc/export_plain_stats-9.3.sql.sample_test
+\i doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             c.relname,
+             c.relpages,
+             c.reltuples,
+             c.relallvisible,
+             (pg_relation_size(c.oid) / current_setting('block_size')::int4)::int4
+                 AS curpages,
+             pg_catalog.pg_stat_get_last_analyze_time(c.oid)
+                 AS last_analyze,
+             pg_catalog.pg_stat_get_last_autoanalyze_time(c.oid)
+                 AS last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             s.stainherit,
+             s.stanullfrac,
+             s.stawidth,
+             s.stadistinct,
+             s.stakind1,
+             s.stakind2,
+             s.stakind3,
+             s.stakind4,
+             s.stakind5,
+             s.staop1,
+             s.staop2,
+             s.staop3,
+             s.staop4,
+             s.staop5,
+             s.stanumbers1,
+             s.stanumbers2,
+             s.stanumbers3,
+             s.stanumbers4,
+             s.stanumbers5,
+             s.stavalues1,
+             s.stavalues2,
+             s.stavalues3,
+             s.stavalues4,
+             s.stavalues5
+        FROM pg_statistic s
+        JOIN pg_attribute a
+          ON (s.starelid = a.attrelid AND s.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN pg_catalog.pg_class c
+          ON s.starelid = c.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON c.relnamespace = n2.oid
+       WHERE c.relkind IN ('r', 'i', 'f', 'm')
+         AND NOT n2.nspname IN ('pg_catalog',
+                                'pg_toast',
+                                'information_schema',
+                                'dbms_stats')
+        -- AND n2.nspname = 'public'
+        -- AND c.relname = 'test'
+        -- AND a.attname = 'id'
+ AND n2.nspname = 's1' AND c.relname IS NULL
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+TRUNCATE dbms_stats.work;
+\! rm doc/export_plain_stats-9.3.sql.sample_test
+/*
+ * No.16-2 export_effective_stats-9.3.sql.sample
+ */
+-- No.16-2-1
+VACUUM ANALYZE;
+SELECT dbms_stats.lock_database_stats();
+ lock_database_stats 
+---------------------
+ pt0
+ pt0_idx
+ st0
+ st0_idx
+ st1
+ s0.st0
+ s0.st0_idx
+ s0.st1
+ s0.st1_idx
+ s0.st2
+ s0.st2_idx
+ st1_idx
+ st1_exp
+ s0.sft0
+ s0.smv0
+ s1.st0
+ s0.st3
+(17 rows)
+
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples, relallvisible, curpages) = (NULL, NULL, NULL, NULL);
+UPDATE dbms_stats._column_stats_locked
+   SET (stanullfrac, stawidth, stadistinct,
+        stakind1, stakind2, stakind3, stakind4, stakind5,
+        staop1, staop2, staop3, staop4, staop5,
+        stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+        stavalues1, stavalues2, stavalues3, stavalues4, stavalues5)
+     = (NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL, NULL)
+ WHERE starelid = 's0.st0'::regclass;
+\i doc/export_effective_stats-9.3.sql.sample
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 |       stavalues3       | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------------------+------------+------------
+ public  | pt0     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ public  | pt0_idx |        2 |         0 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2}                  | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | name    | pg_catalog          | bpchar  |         9 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {"test "}              | (null)     | (null)
+ public  | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ public  | st1     |       45 |     10000 |            45 |       45 | str     | pg_catalog          | text    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {1,0,2}                | (null)     | (null)
+ public  | st1     |       45 |     10000 |            45 |       45 | val     | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,0,2}                | (null)     | (null)
+ public  | st1_exp |       30 |     10000 |             0 |       30 | lower   | pg_catalog          | text    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,0,2}                | (null)     | (null)
+ public  | st1_idx |       30 |     10000 |             0 |       30 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2}                  | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {10,20}                | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |          -3 |       -3 |          -3 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-3,22}     | {-3,23}     | {-3,24}     | {-3,21}     | {-3,25}     | (null)           | (null)     | {1,test}               | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)                 | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {4,5,6}                | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {40,50,60}             | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3}                | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {1,comment,test}       | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3,4}              | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {15,25,35,45}          | (null)     | (null)
+(27 rows)
+
+TRUNCATE dbms_stats.work;
+-- No.16-2-2
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE n2.nspname = 's0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 |       stavalues3       | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2}                  | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {10,20}                | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |          -3 |       -3 |          -3 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-3,22}     | {-3,23}     | {-3,24}     | {-3,21}     | {-3,25}     | (null)           | (null)     | {1,test}               | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)                 | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {4,5,6}                | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {40,50,60}             | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3}                | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {1,comment,test}       | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)                 | (null)     | (null)
+(16 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-3
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'st0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'st0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 |  stavalues3   | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+---------------+------------+------------
+ public  | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2}         | (null)     | (null)
+ public  | st0     |        1 |         2 |             1 |        1 | name    | pg_catalog          | bpchar  |         9 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {"test "}     | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)        | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)        | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)        | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)        | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)           | (null)     | {1,2,3,4}     | (null)     | (null)
+ s1      | st0     |        1 |         4 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)           | (null)     | {15,25,35,45} | (null)     | (null)
+(8 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-4
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'pg_toast_1262\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'pg_toast_1262'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-5
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'st0_idx\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'st0_idx'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ public  | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(2 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-6
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'ss0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'ss0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-7
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'sc0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'sc0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-8
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'sft0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'sft0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |       stavalues3       | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)     | (null)     | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)
+(1 row)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-9
+\! sed '/ORDER/i\\ WHERE cl.relname = '"\'smv0\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE cl.relname = 'smv0'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)     | (null)     | {1,2}      | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | num     | pg_catalog          | int4    |        -1 | f          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | (null)     | (null)     | {10,20}    | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | txt     | pg_catalog          | text    |        -1 | f          |          -3 |       -3 |          -3 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-3,22}     | {-3,23}     | {-3,24}     | {-3,21}     | {-3,25}     | (null)     | (null)     | {1,test}   | (null)     | (null)
+(3 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-10
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND a.attname = '"\'id\'" doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE n2.nspname = 's0' AND a.attname = 'id'
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 |       stavalues3       | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------------------+------------+------------
+ s0      | sft0    |        1 |        10 |             0 |        0 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)      | (null)     | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)
+ s0      | smv0    |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)      | (null)     | {1,2}                  | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)                 | (null)     | (null)
+ s0      | st0     |        1 |         2 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)                 | (null)     | (null)
+ s0      | st1     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)      | (null)     | {4,5,6}                | (null)     | (null)
+ s0      | st2     |        1 |         3 |             1 |        1 | id      | pg_catalog          | int4    |        -1 | f          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | (null)      | (null)     | {1,2,3}                | (null)     | (null)
+(6 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-11
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND a.attname IS NULL' doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE n2.nspname = 's0' AND a.attname IS NULL
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+ s0      | st0_idx |        2 |         2 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st1_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st2_idx |        2 |         3 |             0 |        2 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+ s0      | st3     |        0 |         0 |             0 |        0 |         | (null)              | (null)  |    (null) | (null)     |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
+(4 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+-- No.16-2-12
+\! sed '/ORDER/i\\ WHERE n2.nspname = '"\'s0\'"' AND cl.relname IS NULL' doc/export_effective_stats-9.3.sql.sample > doc/export_effective_stats-9.3.sql.sample_test
+\i doc/export_effective_stats-9.3.sql.sample_test
+/*
+ * If you want the statistics of per-relation or per-column, please modify
+ * nspname, relname, and attname in 'WHERE' clause.
+ */
+BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
+/*
+ * If you want to chage the output file name, please modify the following name.
+ */
+\o export_stats.dmp
+COPY (SELECT n2.nspname,
+             cl.relname,
+             r.relpages,
+             r.reltuples,
+             r.relallvisible,
+             r.curpages,
+             r.last_analyze,
+             r.last_autoanalyze,
+             COALESCE(a.attname, ''),
+             n1.nspname AS schemaname_of_atttype,
+             t.typname,
+             a.atttypmod,
+             co.stainherit,
+             co.stanullfrac,
+             co.stawidth,
+             co.stadistinct,
+             co.stakind1,
+             co.stakind2,
+             co.stakind3,
+             co.stakind4,
+             co.stakind5,
+             co.staop1,
+             co.staop2,
+             co.staop3,
+             co.staop4,
+             co.staop5,
+             co.stanumbers1,
+             co.stanumbers2,
+             co.stanumbers3,
+             co.stanumbers4,
+             co.stanumbers5,
+             co.stavalues1,
+             co.stavalues2,
+             co.stavalues3,
+             co.stavalues4,
+             co.stavalues5
+        FROM dbms_stats.column_stats_effective co
+        JOIN pg_attribute a
+          ON (co.starelid = a.attrelid AND co.staattnum = a.attnum)
+        JOIN pg_type t
+          ON a.atttypid = t.oid
+        JOIN pg_namespace n1
+          ON t.typnamespace = n1.oid
+       RIGHT JOIN dbms_stats.relation_stats_effective r
+          ON co.starelid = r.relid
+        JOIN pg_catalog.pg_class cl
+          ON r.relid = cl.oid
+        JOIN pg_catalog.pg_namespace n2
+          ON cl.relnamespace = n2.oid
+       -- WHERE n2.nspname = 'public'
+       -- AND cl.relname = 'test'
+       -- AND a.attname = 'id'
+ WHERE n2.nspname = 's0' AND cl.relname IS NULL
+       ORDER BY starelid, staattnum)
+TO STDOUT
+(FORMAT 'binary');
+\o
+COMMIT;
+COPY dbms_stats.work FROM '@abs_srcdir@/export_stats.dmp' (FORMAT 'binary');
+SELECT * FROM work_v;
+ nspname | relname | relpages | reltuples | relallvisible | curpages | attname | nspname_of_typename | typname | atttypmod | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
+---------+---------+----------+-----------+---------------+----------+---------+---------------------+---------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
+(0 rows)
+
+\! rm doc/export_effective_stats-9.3.sql.sample_test
+TRUNCATE dbms_stats.work;
+/*
+ * Stab function dbms_stats.import
+ */
+ALTER FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RENAME TO truth_import;
+CREATE FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RETURNS void AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are "%", "%", "%", "%"', $1, $2, $3, $4;
+    RETURN;
+END
+$$
+LANGUAGE plpgsql;
+/*
+ * No.17-1 dbms_stats.import_database_stats(src)
+ */
+-- No.17-1-1
+SELECT dbms_stats.import_database_stats('@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "<NULL>", "<NULL>", "<NULL>", "@abs_srcdir@/export_stats.dmp"
+ import_database_stats 
+-----------------------
+(1 row)
+
+/*
+ * No.17-2 dbms_stats.import_schema_stats(schemaname, src)
+ */
+-- No.17-2-1
+SELECT dbms_stats.import_schema_stats('s0', '@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "s0", "<NULL>", "<NULL>", "@abs_srcdir@/export_stats.dmp"
+ import_schema_stats 
+---------------------
+(1 row)
+
+/*
+ * No.17-3 dbms_stats.import_table_stats(relid, src)
+ */
+-- No.17-3-1
+SELECT dbms_stats.import_table_stats('s0.st0', '@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "<NULL>", "s0.st0", "<NULL>", "@abs_srcdir@/export_stats.dmp"
+ import_table_stats 
+--------------------
+(1 row)
+
+/*
+ * No.17-4 dbms_stats.import_table_stats(schemaname, tablename, src)
+ */
+-- No.17-4-1
+SELECT dbms_stats.import_table_stats('s0', 'st0', '@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "<NULL>", "s0.st0", "<NULL>", "@abs_srcdir@/export_stats.dmp"
+ import_table_stats 
+--------------------
+(1 row)
+
+/*
+ * No.17-5 dbms_stats.import_column_stats (relid, attname, src)
+ */
+-- No.17-5-1
+SELECT dbms_stats.import_column_stats('s0.st0', 'id', '@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "<NULL>", "s0.st0", "id", "@abs_srcdir@/export_stats.dmp"
+ import_column_stats 
+---------------------
+(1 row)
+
+/*
+ * No.17-6 dbms_stats.import_column_stats (schemaname, tablename, attname, src)
+ */
+-- No.17-6-1
+SELECT dbms_stats.import_column_stats('s0', 'st0', 'id','@abs_srcdir@/export_stats.dmp');
+NOTICE:  arguments are "<NULL>", "s0.st0", "id", "@abs_srcdir@/export_stats.dmp"
+ import_column_stats 
+---------------------
+(1 row)
+
+/*
+ * Delete stab function dbms_stats.import
+ */
+DROP FUNCTION dbms_stats.import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+);
+ALTER FUNCTION dbms_stats.truth_import(
+    nspname text,
+    relid regclass,
+    attname text,
+    src text
+) RENAME TO import;
index 32bee75..ab49b8f 100644 (file)
@@ -1,7 +1,7 @@
 # pg_dbms_stats extension
 
 comment = 'stabilize plans by fixing statistics'
-default_version = '1.3.4'
+default_version = '1.3.5'
 module_pathname = '$libdir/pg_dbms_stats'
 relocatable = false
 schema = dbms_stats
diff --git a/sql/init-9.4.sql b/sql/init-9.4.sql
new file mode 100644 (file)
index 0000000..6a5b537
--- /dev/null
@@ -0,0 +1,113 @@
+CREATE MATERIALIZED VIEW s0.smv0 AS
+SELECT st0.id,
+       st0.num,
+       st2.txt
+  FROM s0.st0,s0.st2
+ WHERE st0.id = st2.id
+ ORDER BY id;
+CREATE VIEW plain_relations_statistic_v AS
+SELECT oid::regclass,
+       relpages,
+       reltuples,
+       relallvisible,
+       pg_relation_size(oid) / 8192 curpages
+  FROM pg_class
+ ORDER BY oid::regclass::text;
+CREATE VIEW relations_locked_v AS
+SELECT relid::regclass,
+       relname,
+       relpages,
+       reltuples,
+       relallvisible,
+       curpages
+  FROM dbms_stats._relation_stats_locked
+ ORDER BY relid;
+CREATE VIEW relations_backup_v AS
+SELECT id,
+       relid::regclass,
+       relname,
+       relpages,
+       reltuples,
+       relallvisible,
+       curpages
+  FROM dbms_stats.relation_stats_backup
+ ORDER BY id, relid;
+CREATE VIEW plain_columns_statistic_v AS
+SELECT starelid::regclass, staattnum, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1::text, stavalues2::text, stavalues3::text, stavalues4::text, stavalues5::text
+  FROM pg_statistic
+ ORDER BY starelid, staattnum, stainherit;
+CREATE VIEW columns_locked_v AS
+SELECT starelid::regclass, staattnum, attname, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats._column_stats_locked c
+  JOIN pg_attribute a
+    ON (c.starelid = a.attrelid AND c.staattnum = a.attnum)
+ ORDER BY starelid, staattnum, stainherit;
+CREATE VIEW columns_backup_v AS
+SELECT id, statypid,
+       starelid::regclass, staattnum, stainherit,
+       stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats.column_stats_backup
+ ORDER BY id, starelid, staattnum, stainherit;
+CREATE TABLE dbms_stats.work (
+  nspname          name   NOT NULL,
+  relname          name   NOT NULL,
+  relpages         int4   NOT NULL,
+  reltuples        float4 NOT NULL,
+  relallvisible    int4   NOT NULL,
+  curpages         int4   NOT NULL,
+  last_analyze     timestamp with time zone,
+  last_autoanalyze timestamp with time zone,
+  attname          name,
+  nspname_of_typename name,
+  typname          name,
+  atttypmod        int4,
+  stainherit       bool,
+  stanullfrac      float4,
+  stawidth         int4,
+  stadistinct      float4,
+  stakind1         int2,
+  stakind2         int2,
+  stakind3         int2,
+  stakind4         int2,
+  stakind5         int2,
+  staop1           oid,
+  staop2           oid,
+  staop3           oid,
+  staop4           oid,
+  staop5           oid,
+  stanumbers1      float4[],
+  stanumbers2      float4[],
+  stanumbers3      float4[],
+  stanumbers4      float4[],
+  stanumbers5      float4[],
+  stavalues1       dbms_stats.anyarray,
+  stavalues2       dbms_stats.anyarray,
+  stavalues3       dbms_stats.anyarray,
+  stavalues4       dbms_stats.anyarray
+ ,stavalues5       dbms_stats.anyarray
+);
+CREATE VIEW work_v AS
+SELECT nspname, relname, relpages, reltuples, relallvisible,
+       curpages, attname, nspname_of_typename, typname, atttypmod,
+       stainherit, stanullfrac, stawidth, stadistinct,
+       stakind1, stakind2, stakind3, stakind4, stakind5,
+       staop1, staop2, staop3, staop4, staop5,
+       stanumbers1, stanumbers2, stanumbers3, stanumbers4, stanumbers5,
+       stavalues1, stavalues2, stavalues3, stavalues4, stavalues5
+  FROM dbms_stats.work
+ ORDER BY nspname, relname, attname, stainherit;
+ANALYZE s0.sft0;
diff --git a/sql/ut-9.4.sql b/sql/ut-9.4.sql
new file mode 100644 (file)
index 0000000..dd067ca
--- /dev/null
@@ -0,0 +1,2735 @@
+\pset null '(null)'
+/*
+ * No.2-1 table definitions.
+ */
+-- No.2-1-1
+\d dbms_stats.backup_history
+-- No.2-1-2
+\d dbms_stats.column_stats_backup
+-- No.2-1-3
+\d dbms_stats._column_stats_locked
+-- No.2-1-4
+\d dbms_stats.relation_stats_backup
+-- No.2-1-5
+\d dbms_stats._relation_stats_locked
+
+/*
+ * No.2-2 view definitions.
+ */
+-- No.2-2-1
+\dS+ dbms_stats.column_stats_effective
+-- No.2-2-2
+\dS+ dbms_stats.relation_stats_effective
+-- No.2-2-3
+\dS+ dbms_stats.stats
+-- No.2-2-4
+\dS+ dbms_stats.column_stats_locked
+-- No.2-2-5
+\dS+ dbms_stats.relation_stats_locked
+
+/*
+ * No.2-4 dbms_stats.anyarray
+ */
+-- No.2-4-1
+SELECT n.nspname, t.typname, t.typlen, t.typbyval, t.typtype,
+       t.typcategory, t.typispreferred, t.typisdefined, t.typdelim,
+       t.typrelid, t.typelem, t.typinput, t.typoutput, t.typreceive,
+       t.typsend, t.typmodin, t.typmodout, t.typanalyze, t.typalign,
+       t.typstorage, t.typnotnull, t.typbasetype, t.typtypmod, t.typndims,
+       t.typcollation, t.typdefaultbin, t.typdefault, t.typacl
+  FROM pg_type t, pg_namespace n
+ WHERE t.typnamespace = n.oid
+   AND n.nspname = 'dbms_stats'
+   AND t.typname = 'anyarray';
+
+/*
+ * No.5-1 dbms_stats.merge
+ */
+UPDATE pg_statistic SET
+    stanullfrac = staattnum,
+    stawidth = staattnum,
+    stadistinct = staattnum,
+    stakind1 = 4,
+    stakind2 = 1,
+    stakind3 = 2,
+    stakind4 = 3,
+    stakind5 = 5,
+    staop1 = 14,
+    staop2 = 11,
+    staop3 = 12,
+    staop4 = 13,
+    staop5 = 15,
+    stanumbers1 = ARRAY[staattnum,4],
+    stanumbers2 = ARRAY[staattnum,1],
+    stanumbers3 = ARRAY[staattnum,2],
+    stanumbers4 = ARRAY[staattnum,3],
+    stanumbers5 = ARRAY[staattnum,5],
+    stavalues2 = array_cat(stavalues1,stavalues1),
+    stavalues3 = array_cat(array_cat(stavalues1,stavalues1),stavalues1),
+    stavalues4 = array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1)
+   ,stavalues5 = array_cat(array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1),stavalues1)
+ WHERE starelid = 'st0'::regclass;
+SELECT dbms_stats.lock_table_stats('st0');
+UPDATE dbms_stats._column_stats_locked SET
+    stainherit = 't',
+    stanullfrac = -staattnum,
+    stawidth = -staattnum,
+    stadistinct = -staattnum,
+    stakind1 = 2,
+    stakind2 = 3,
+    stakind3 = 4,
+    stakind4 = 1,
+    stakind5 = 5,
+    staop1 = 22,
+    staop2 = 23,
+    staop3 = 24,
+    staop4 = 21,
+    staop5 = 25,
+    stanumbers1 = ARRAY[-staattnum,22],
+    stanumbers2 = ARRAY[-staattnum,23],
+    stanumbers3 = ARRAY[-staattnum,24],
+    stanumbers4 = ARRAY[-staattnum,21],
+    stanumbers5 = ARRAY[-staattnum,25],
+    stavalues1 = stavalues3,
+    stavalues2 = stavalues2,
+    stavalues3 = stavalues1,
+    stavalues4 = stavalues4
+   ,stavalues5 = stavalues5
+;
+
+/*
+ * Driver function dbms_stats.merge1
+ */
+CREATE FUNCTION dbms_stats.merge1(
+    lhs dbms_stats._column_stats_locked,
+    rhs pg_catalog.pg_statistic
+) RETURNS integer AS
+'$libdir/pg_dbms_stats', 'dbms_stats_merge'
+LANGUAGE C STABLE;
+
+SELECT * FROM columns_locked_v
+ WHERE starelid = 'st0'::regclass;
+SELECT * FROM plain_columns_statistic_v
+ WHERE starelid = 'st0'::regclass;
+
+SET client_min_messages TO LOG;
+
+-- No.5-1-1
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(NULL, s)
+         FROM pg_statistic s
+        WHERE starelid = 'st0'::regclass
+          AND staattnum = '1'::int2) m;
+
+-- No.5-1-2
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, NULL)
+         FROM dbms_stats._column_stats_locked v
+        WHERE starelid = 'st0'::regclass
+          AND staattnum = '2'::int2) m;
+
+-- No.5-1-3
+SELECT dbms_stats.merge(NULL, NULL);
+
+-- No.5-1-4
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-5
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-6
+SELECT dbms_stats.merge1(v, s)
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+
+-- No.5-1-7
+SELECT dbms_stats.merge(NULL, (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM pg_statistic s
+ WHERE s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+
+-- No.5-1-8
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(NULL, (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM pg_statistic s
+        WHERE s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-9
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), NULL)
+  FROM dbms_stats._column_stats_locked v
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2;
+
+-- No.5-1-10
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), NULL)
+         FROM dbms_stats._column_stats_locked v
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2) m;
+
+-- No.5-1-11
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+
+-- No.5-1-12
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-13
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+             NULL, NULL, NULL,
+             NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL
+            ,NULL, NULL, NULL, NULL
+             ), s)
+         FROM pg_statistic s
+        WHERE s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-14
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, (
+             NULL, NULL, NULL,
+             NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL,
+             NULL, NULL, NULL, NULL))
+         FROM dbms_stats._column_stats_locked v
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2) m;
+
+-- No.5-1-15
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge(v, s)
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-16
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL
+      ,NULL, NULL, NULL, NULL
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL,
+       NULL, NULL, NULL, NULL))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '2'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+
+-- No.5-1-17
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+              v.stakind5,
+              v.staop1, v.staop2, v.staop3,
+              v.staop4,
+              NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+              s.stakind5,
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-18
+SELECT dbms_stats.merge((
+       v.starelid::regclass, v.staattnum, v.stainherit,
+       v.stanullfrac, v.stawidth, v.stadistinct,
+       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
+       v.stakind5,
+       v.staop1, v.staop2, v.staop3,
+       v.staop4,
+       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+       v.stanumbers5,
+       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+      ,v.stavalues5
+       ), (
+       s.starelid::regclass, s.staattnum, s.stainherit,
+       s.stanullfrac, s.stawidth, s.stadistinct,
+       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
+       s.stakind5,
+       s.staop1, s.staop2, s.staop3,
+       s.staop4,
+       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+       s.stanumbers5,
+       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+      ,s.stavalues5
+       ))
+  FROM dbms_stats._column_stats_locked v,
+       pg_statistic s
+ WHERE v.starelid = 'st0'::regclass
+   AND v.staattnum = '1'::int2
+   AND s.starelid = 'st0'::regclass
+   AND s.staattnum = '1'::int2;
+
+-- No.5-1-19
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((
+              v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-20
+SELECT (m.merge).starelid::regclass,
+       (m.merge).staattnum,
+       (m.merge).stainherit,
+       (m.merge).stanullfrac,
+       (m.merge).stawidth,
+       (m.merge).stadistinct,
+       (m.merge).stakind1,
+       (m.merge).stakind2,
+       (m.merge).stakind3,
+       (m.merge).stakind4,
+       (m.merge).stakind5,
+       (m.merge).staop1,
+       (m.merge).staop2,
+       (m.merge).staop3,
+       (m.merge).staop4,
+       (m.merge).staop5,
+       (m.merge).stanumbers1,
+       (m.merge).stanumbers2,
+       (m.merge).stanumbers3,
+       (m.merge).stanumbers4,
+       (m.merge).stanumbers5,
+       (m.merge).stavalues1,
+       (m.merge).stavalues2,
+       (m.merge).stavalues3,
+       (m.merge).stavalues4
+      ,(m.merge).stavalues5
+ FROM (SELECT dbms_stats.merge((v.starelid::regclass, v.staattnum, v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '2'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2) m;
+
+-- No.5-1-21
+SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '1', '1', '1', '1',
+              '1',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2;
+
+-- No.5-1-22
+SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
+              v.stanullfrac, v.stawidth, v.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              v.staop1, v.staop2, v.staop3, v.staop4,
+              v.staop5,
+              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
+              v.stanumbers5,
+              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
+             ,v.stavalues5
+              ), (
+              s.starelid::regclass, s.staattnum, s.stainherit,
+              s.stanullfrac, s.stawidth, s.stadistinct,
+              '2', '2', '2', '2',
+              '2',
+              s.staop1, s.staop2, s.staop3, s.staop4,
+              s.staop5,
+              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
+              s.stanumbers5,
+              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
+             ,s.stavalues5
+              ))
+         FROM dbms_stats._column_stats_locked v,
+              pg_statistic s
+        WHERE v.starelid = 'st0'::regclass
+          AND v.staattnum = '1'::int2
+          AND s.starelid = 'st0'::regclass
+          AND s.staattnum = '1'::int2;
+RESET client_min_messages;
+SELECT dbms_stats.unlock_database_stats();
+
+/*
+ * No.6-4 dbms_stats.is_target_relkind
+ */
+-- No.6-4- 10-11
+SELECT dbms_stats.is_target_relkind(k::"char")
+ FROM (VALUES ('r'), ('i'), ('f'), ('m'),
+                         ('S'), ('t'), ('v'), ('c')) t(k);
+
+/*
+ * No.7-1 dbms_stats.backup
+ */
+DELETE FROM dbms_stats.backup_history;
+INSERT INTO dbms_stats.backup_history(id, time, unit) values(1, '2012-01-01', 'd');
+-- No.7-1-9
+SELECT dbms_stats.backup(1, 's0.sft0'::regclass, NULL);
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.7-1-10
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, 's0.smv0'::regclass, NULL);
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.7-1-12
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, NULL, 1::int2);
+SELECT relid::regclass FROM dbms_stats.relation_stats_backup
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+
+-- No.7-1-14
+DELETE FROM dbms_stats.relation_stats_backup;
+SELECT dbms_stats.backup(1, NULL::regclass, NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_backup
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+
+-- No.7-1-18
+DELETE FROM dbms_stats.relation_stats_backup;
+\! psql contrib_regression -c "SELECT dbms_stats.backup(NULL, 's0.st0'::regclass, NULL)" > results/ut_no2_1_17.out 2>&1
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+/*
+ * No.8-1 dbms_stats.backup
+ */
+SELECT setval('dbms_stats.backup_history_id_seq',1, false);
+/*
+ * Stab function dbms_stats.backup
+ */
+ALTER FUNCTION dbms_stats.backup(
+    backup_id int8,
+    relid regclass,
+    attnum int2)
+    RENAME TO truth_func_backup;
+
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    backup_id int8,
+    regclass,
+    attnum int2)
+RETURNS int8 AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
+    RETURN 1;
+END;
+$$
+LANGUAGE plpgsql;
+
+-- No.8-1-1
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-2
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-3
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(NULL::regclass, 'id', 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-4
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(NULL::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-5
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(0, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-6
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-7
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup(
+    'pg_toast.pg_toast_2618'::regclass,
+    NULL,
+    'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-8
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0_idx'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-9
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.ss0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-10
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.sc0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-11
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.sft0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-12
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.smv0'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-13
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('pg_catalog.pg_class'::regclass, NULL, 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-14
+DELETE FROM dbms_stats.backup_history;
+SELECT dbms_stats.backup('s0.st0'::regclass, 'dummy', 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+-- No.8-1-15
+DELETE FROM dbms_stats.backup_history;
+DELETE FROM pg_statistic
+ WHERE starelid = 's0.st0'::regclass
+   AND staattnum = 1::int2;
+SELECT count(*) FROM dbms_stats.column_stats_effective
+ WHERE starelid = 's0.st0'::regclass
+   AND staattnum = 1::int2;
+SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history;
+
+/*
+ * Stab function dbms_stats.backup
+ */
+ALTER FUNCTION dbms_stats.backup(
+    relid regclass,
+    attname text,
+    comment text)
+    RENAME TO truth_func_backup;
+CREATE OR REPLACE FUNCTION dbms_stats.backup(
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL,
+    comment text DEFAULT NULL)
+RETURNS int8 AS
+$$
+BEGIN
+    IF $3 = '<NULL>' THEN
+        RAISE NOTICE 'third argument is not NULL but string "<NULL>"';
+    END IF;
+    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
+    RETURN 1;
+END;
+$$
+LANGUAGE plpgsql;
+
+/*
+ * No.8-3 dbms_stats.backup_schema_stats
+ */
+SELECT setval('dbms_stats.backup_history_id_seq',9, false);
+-- No.8-3-1
+SELECT dbms_stats.backup_schema_stats('s0', 'comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+-- No.8-3-2
+SELECT dbms_stats.backup_schema_stats('s00', 'comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+-- No.8-3-3
+SELECT dbms_stats.backup_schema_stats('pg_catalog', 'comment');
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ ORDER BY id DESC
+ LIMIT 1;
+
+/*
+ * Delete stab function dbms_stats.backup
+ */
+DROP FUNCTION dbms_stats.backup(
+    backup_id int8,
+    regclass,
+    attnum int2);
+ALTER FUNCTION dbms_stats.truth_func_backup(
+    backup_id int8,
+    regclass,
+    attnum int2)
+    RENAME TO backup;
+DROP FUNCTION dbms_stats.backup(
+    regclass,
+    attname text,
+    comment text);
+ALTER FUNCTION dbms_stats.truth_func_backup(
+    regclass,
+    attname text,
+    comment text)
+    RENAME TO backup;
+VACUUM ANALYZE;
+
+/*
+ * create backup statistics state A
+ */
+DELETE FROM dbms_stats.backup_history;
+
+INSERT INTO dbms_stats.backup_history(id, time, unit)
+    VALUES (1, '2012-02-29 23:59:56.999999', 'd');
+
+SELECT setval('dbms_stats.backup_history_id_seq',1);
+SELECT dbms_stats.backup();
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:57'
+ WHERE id = 2;
+SELECT dbms_stats.backup('s0.st0');
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:57.000001'
+ WHERE id = 3;
+SELECT dbms_stats.backup();
+UPDATE dbms_stats.backup_history
+   SET time = '2012-02-29 23:59:58'
+ WHERE id = 4;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 4;
+SELECT dbms_stats.backup('s0.st0', 'id');
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:00'
+ WHERE id = 5;
+SELECT dbms_stats.backup('s0.st0');
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:02'
+ WHERE id = 6;
+SELECT dbms_stats.backup('public.st0');
+UPDATE dbms_stats.backup_history
+   SET time = '2012-03-01 00:00:04'
+ WHERE id = 7;
+INSERT INTO dbms_stats.backup_history(time, unit)
+    VALUES ('2012-03-01 00:00:06', 's');
+SELECT dbms_stats.backup(8, c.oid, NULL)
+  FROM pg_catalog.pg_class c,
+       pg_catalog.pg_namespace n
+ WHERE n.nspname = 's0'
+   AND c.relnamespace = n.oid
+   AND c.relkind IN ('r', 'i');
+
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT * FROM relations_backup_v;
+SELECT * FROM columns_backup_v;
+
+VACUUM ANALYZE;
+
+/*
+ * No.9-1 dbms_stats.restore
+ */
+-- No.9-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 'st0', NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's00.s0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(NULL, 's0.st0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', 'id');
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, NULL, 'id');
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, NULL, NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(0, 's0.st0', NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 0, 'id');
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(1, 's0.st0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', 'dummy');
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(1, 's0.st0', 'id');
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+
+-- No.9-1-15
+DELETE FROM dbms_stats._relation_stats_locked;
+ALTER TABLE s1.st0 DROP COLUMN id;
+SELECT dbms_stats.restore(2, 's1.st0', 'id');
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-14
+DELETE FROM dbms_stats._relation_stats_locked;
+\set s1_st0_oid `psql contrib_regression -tA -c "SELECT c.oid FROM pg_class c, pg_namespace n WHERE c.relnamespace = n.oid AND n.nspname = 's1' AND c.relname = 'st0';"`
+DROP TABLE s1.st0;
+-- SELECT dbms_stats.restore(2, :s1_st0_oid, NULL);
+-- To avoid test unstability caused by relation id alloction, the test
+-- above is omitted.
+
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+CREATE TABLE s1.st0(id integer, num integer);
+INSERT INTO s1.st0 VALUES (1, 15), (2, 25), (3, 35), (4, 45);
+VACUUM ANALYZE;
+-- No.9-1-16
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-17
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2,
+             'pg_toast.pg_toast_2618'::regclass,
+             'pg_toast.pg_toast_2618', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 'pg_toast.pg_toast_2618';
+SELECT dbms_stats.restore(2, 'pg_toast.pg_toast_2618', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 'pg_toast.pg_toast_2618';
+
+-- No.9-1-18
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore(2, 's0.st0_idx', NULL);
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.9-1-19
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 's0.ss0'::regclass, 's0.ss0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 's0.ss0';
+SELECT dbms_stats.restore(2, 's0.ss0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 's0.ss0';
+
+-- No.9-1-20
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 's0.sc0'::regclass, 's0.sc0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 's0.sc0';
+SELECT dbms_stats.restore(2, 's0.sc0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 's0.sc0';
+
+-- No.9-1-21
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (3, 's0.sft0'::regclass, 's0.sft0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 3
+   AND relname = 's0.sft0';
+SELECT dbms_stats.restore(2, 's0.sft0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 3
+   AND relname = 's0.sft0';
+
+-- No.9-1-22
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (3, 's0.smv0'::regclass, 's0.smv0', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 3
+   AND relname = 's0.smv0';
+SELECT dbms_stats.restore(2, 's0.smv0', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 3
+   AND relname = 's0.smv0';
+
+-- No.9-1-23
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats.relation_stats_backup(
+               id, relid, relname, relpages, reltuples,
+               relallvisible,
+               curpages)
+     VALUES (2, 'pg_catalog.pg_class'::regclass, 'pg_catalog.pg_class', 1, 1,
+             1,
+             1);
+SELECT * FROM relations_backup_v
+ WHERE id = 2
+   AND relname = 'pg_catalog.pg_class';
+SELECT dbms_stats.restore(2, 'pg_catalog.pg_class', NULL);
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT count(*) FROM dbms_stats.relation_stats_locked;
+DELETE FROM dbms_stats.relation_stats_backup
+ WHERE id = 2
+   AND relname = 'pg_catalog.pg_class';
+
+-- No.9-1-24
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(relid, relname)
+    VALUES ('s0.st0'::regclass, 's0.st0');
+INSERT INTO dbms_stats._column_stats_locked(starelid, staattnum, stainherit)
+     SELECT starelid::regclass, staattnum, stainherit
+       FROM dbms_stats.column_stats_effective
+      WHERE starelid = 's0.st0'::regclass;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 2;
+SELECT * FROM columns_locked_v;
+SELECT * FROM relations_locked_v;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v;
+
+-- No.9-1-25
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 2;
+SELECT dbms_stats.restore(2, 's0.st0', NULL);
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v;
+
+/*
+ * Stab function dbms_stats.restore
+ */
+CREATE OR REPLACE FUNCTION dbms_stats.restore(
+    backup_id int8,
+    relid regclass DEFAULT NULL,
+    attname text DEFAULT NULL)
+RETURNS SETOF regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are "%, %, %"', $1, $2, $3;
+    RETURN QUERY
+        SELECT c.oid::regclass
+          FROM pg_class c, dbms_stats.relation_stats_backup b
+         WHERE (c.oid = $2 OR $2 IS NULL)
+           AND c.oid = b.relid
+           AND c.relkind IN ('r', 'i')
+           AND (b.id <= $1 OR $1 IS NOT NULL)
+         GROUP BY c.oid
+         ORDER BY c.oid::regclass::text;
+END;
+$$
+LANGUAGE plpgsql;
+
+/*
+ * No.10-1 dbms_stats.restore_database_stats
+ */
+-- No.10-1-1
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
+-- No.10-1-2
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57.000002');
+-- No.10-1-3
+SELECT dbms_stats.restore_database_stats('2012-01-01 00:00:00');
+--#No.10-1-4 is skipped after lock tests
+--#No.10-1-5 is skipped after lock tests
+-- No.10-1-6
+SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
+
+/*
+ * No.10-2 dbms_stats.restore_schema_stats
+ */
+-- No.10-2-1
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+-- No.10-2-2
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57.000002');
+-- No.10-2-3
+SELECT dbms_stats.restore_schema_stats('s0', '2012-01-01 00:00:00');
+--#No.10-2-4 is skipped after lock tests
+--#No.10-2-5 is skipped after lock tests
+-- No.10-2-6
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+-- No.10-2-7
+SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
+--#No.10-2-8 is skipped after lock tests
+-- No.10-2-9
+SELECT dbms_stats.restore_schema_stats('s00', '2012-02-29 23:59:57');
+-- No.10-2-10
+SELECT dbms_stats.restore_schema_stats('pg_catalog', '2012-02-29 23:59:57');
+
+/*
+ * No.10-7 dbms_stats.restore_stats
+ */
+-- No.10-7-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore_stats(NULL);
+
+-- No.10-7-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.restore_stats(0);
+
+-- No.10-7-3
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.restore_stats(2);
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT relid::regclass FROM dbms_stats.relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.10-7-4
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(relid, relname)
+     SELECT relid::regclass, relname
+       FROM dbms_stats.relation_stats_effective;
+INSERT INTO dbms_stats._column_stats_locked(starelid, staattnum, stainherit)
+     SELECT starelid::regclass, staattnum, stainherit
+       FROM dbms_stats.column_stats_effective;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 8;
+SELECT * FROM columns_locked_v;
+SELECT * FROM relations_locked_v;
+SELECT dbms_stats.restore_stats(8);
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v;
+
+-- No.10-7-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT id, unit, comment FROM dbms_stats.backup_history
+ WHERE id = 8;
+SELECT dbms_stats.restore_stats(8);
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v;
+
+/*
+ * No.11-1 dbms_stats.lock(relid, attname)
+ */
+-- No.11-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL, NULL);
+-- No.11-1-2
+ALTER FUNCTION dbms_stats.lock(relid regclass)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(relid regclass)
+RETURNS regclass AS
+$$
+BEGIN
+       RAISE NOTICE 'arguments are %', $1;
+       RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', NULL);
+DROP FUNCTION dbms_stats.lock(relid regclass);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
+    RENAME TO lock;
+-- No.11-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL, 'id');
+-- No.11-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(0, 'id');
+-- No.11-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_toast.pg_toast_2618', 'id');
+-- No.11-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0_idx', 'id');
+-- No.11-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('st1_exp', 'lower');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+DELETE FROM dbms_stats._relation_stats_locked;
+
+-- No.11-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.ss0', 'id');
+-- No.11-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sc0', 'id');
+-- No.11-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sft0', 'id');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.smv0', 'id');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-1-14
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_catalog.pg_class', 'id');
+-- No.11-1-15
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'dummy');
+-- No.11-1-16
+DELETE FROM dbms_stats._relation_stats_locked;
+DELETE FROM pg_statistic
+ WHERE starelid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0', 'id');
+VACUUM ANALYZE;
+-- No.11-1-17
+DELETE FROM dbms_stats._relation_stats_locked;
+INSERT INTO dbms_stats._relation_stats_locked(
+    relid, relname, relpages, reltuples,
+    relallvisible,
+    curpages)
+    VALUES('s0.st0'::regclass, 's0.st0', 1, 1640,
+           1,
+           1);
+SELECT dbms_stats.lock_column_stats('s0.st0','id');
+UPDATE dbms_stats._column_stats_locked
+   SET (stanullfrac, stawidth, stadistinct,
+        stakind1, stakind2, stakind3, stakind4,
+        stakind5,
+        staop1, staop2, staop3, staop4,
+        staop5,
+        stanumbers1, stanumbers2, stanumbers3, stanumbers4,
+        stanumbers5,
+        stavalues1, stavalues2, stavalues3, stavalues4
+       ,stavalues5
+       ) = (
+        NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL,
+        NULL, NULL, NULL, NULL)
+ WHERE starelid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0', 'id');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-1-18
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0', 'id');
+SELECT * FROM relations_locked_v
+ WHERE relid = 's0.st0'::regclass;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+/*
+ * No.11-2 dbms_stats.lock(relid)
+ */
+-- No.11-2-1
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.lock('s0.st0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+SELECT * FROM internal_locks;
+COMMIT;
+
+-- No.11-2-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock(NULL);
+-- No.11-2-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('0');
+-- No.11-2-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-2-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_toast.pg_toast_2618');
+-- No.11-2-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0_idx');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-2-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.ss0');
+-- No.11-2-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sc0');
+-- No.11-2-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.sft0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-2-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.smv0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-2-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('pg_catalog.pg_class');
+-- No.11-2-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_table_stats('s0.st0');
+UPDATE dbms_stats._relation_stats_locked
+   SET (relpages, reltuples,
+        relallvisible,
+        curpages)
+     = (NULL, NULL, NULL
+       ,NULL
+       )
+ WHERE relid = 's0.st0'::regclass;
+SELECT dbms_stats.lock('s0.st0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+-- No.11-2-13
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock('s0.st0');
+SELECT * FROM relations_locked_v;
+SELECT * FROM columns_locked_v c;
+
+/*
+ * Stab function dbms_stats.lock
+ */
+ALTER FUNCTION dbms_stats.lock(relid regclass)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(relid regclass)
+RETURNS regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %', $1;
+    RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+
+ALTER FUNCTION dbms_stats.lock(relid regclass, attname text)
+    RENAME TO truth_lock;
+CREATE FUNCTION dbms_stats.lock(
+    relid regclass,
+    attname text)
+RETURNS regclass AS
+$$
+BEGIN
+    RAISE NOTICE 'arguments are %, %', $1, $2;
+    RETURN $1;
+END
+$$
+LANGUAGE plpgsql;
+
+/*
+ * No.12-1 dbms_stats.lock_database_stats
+ */
+-- No.12-1-1
+SELECT dbms_stats.lock_database_stats();
+
+/*
+ * No.12-2 dbms_stats.lock_schema_stats
+ */
+-- No.12-2-1
+SELECT dbms_stats.lock_schema_stats('s0');
+-- No.12-2-2
+SELECT dbms_stats.lock_schema_stats('s00');
+-- No.12-2-3
+SELECT dbms_stats.lock_schema_stats('pg_catalog');
+
+/*
+ * No.12-3 dbms_stats.lock_table_stats(regclass)
+ */
+-- No.12-3-1
+SELECT dbms_stats.lock_table_stats('s0.st0');
+-- No.12-3-2
+SELECT dbms_stats.lock_table_stats('st0');
+-- No.12-3-3
+SELECT dbms_stats.lock_table_stats('s00.s0');
+
+/*
+ * No.12-4 dbms_stats.lock_table_stats(schemaname, tablename)
+ */
+-- No.12-4-1
+SELECT dbms_stats.lock_table_stats('s0', 'st0');
+
+/*
+ * No.12-5 dbms_stats.lock_column_stats(regclass, attname)
+ */
+-- No.12-5-1
+SELECT dbms_stats.lock_column_stats('s0.st0', 'id');
+-- No.12-5-2
+SELECT dbms_stats.lock_column_stats('st0', 'id');
+-- No.12-5-3
+SELECT dbms_stats.lock_column_stats('s00.s0', 'id');
+
+/*
+ * No.12-6 dbms_stats.lock_column_stats(schemaname, tablename, int2)
+ */
+-- No.12-6-1
+SELECT dbms_stats.lock_column_stats('s0', 'st0', 'id');
+
+/*
+ * Delete Stab function lock
+ */
+DROP FUNCTION dbms_stats.lock(relid regclass);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
+    RENAME TO lock;
+DROP FUNCTION dbms_stats.lock(relid regclass, attname text);
+ALTER FUNCTION dbms_stats.truth_lock(relid regclass, attname text)
+    RENAME TO lock;
+
+/*
+ * No.13-1 dbms_stats.unlock
+ */
+-- No.13-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT dbms_stats.unlock();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.13-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT dbms_stats.unlock();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.13-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT dbms_stats.unlock();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.13-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.unlock();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.13-1-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock('s0.st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.13-1-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock('st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.13-1-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+SELECT dbms_stats.unlock('s00.s0');
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.13-1-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock('s0.st0', 'id');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.13-1-9
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock('s0.st0', 'dummy');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+
+-- No.13-1-10
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT dbms_stats.unlock('s0.st0', 'id');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+
+-- No.13-1-11
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+SELECT dbms_stats.unlock(NULL, 'id');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+
+-- No.13-1-12
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+SELECT dbms_stats.unlock('s0.st0', NULL);
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, staattnum FROM dbms_stats._column_stats_locked
+ GROUP BY starelid, staattnum
+ ORDER BY starelid, staattnum;
+
+-- No.13-1-13
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock();
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+/*
+ * No.14-1 dbms_stats.unlock_database_stats
+ */
+-- No.14-1-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+SELECT dbms_stats.unlock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-1-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.unlock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.14-1-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.unlock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+-- No.14-1-4
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_database_stats();
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT count(*) FROM dbms_stats._relation_stats_locked;
+SELECT count(*) FROM dbms_stats._column_stats_locked;
+
+/*
+ * No.14-2 dbms_stats.unlock_schema_stats
+ */
+-- No.14-2-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('s0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-2-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('s0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('s0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('s0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('s00');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats('pg_catalog');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_schema_stats(NULL);
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-2-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_schema_stats('s0');
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+/*
+ * No.14-3 dbms_stats.unlock_table_stats(regclass)
+ */
+-- No.14-3-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-3-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s00.s0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats(NULL);
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-3-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_table_stats('s0.st0');
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+/*
+ * No.14-4 dbms_stats.unlock_table_stats(schemaname, tablename)
+ */
+-- No.14-4-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0','st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-4-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s00', 's0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats(NULL, 'st0');
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+SELECT dbms_stats.unlock_table_stats('s0', NULL);
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+-- No.14-4-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_table_stats('s0', 'st0');
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid::regclass, count(*) FROM dbms_stats._column_stats_locked
+ GROUP BY starelid
+ ORDER BY starelid;
+
+/*
+ * No.14-5 dbms_stats.unlock_column_stats(regclass, attname)
+ */
+-- No.14-5-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-5-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+SELECT count(*) FROM dbms_stats.column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'dummy');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s00.s0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats(NULL, 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-8
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0.st0', NULL);
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-5-9
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+/*
+ * No.14-6 dbms_stats.unlock_column_stats(schemaname, tablename, attname)
+ */
+-- No.14-6-1
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT * FROM dbms_stats.backup_history
+ ORDER BY id;
+SELECT count(*) FROM dbms_stats.relation_stats_backup;
+SELECT count(*) FROM dbms_stats.column_stats_backup;
+
+-- No.14-6-2
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+DELETE FROM dbms_stats._column_stats_locked;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-3
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-4
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'dummy');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-5
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats(NULL, 'st0', 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-6
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0', NULL, 'id');
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-7
+DELETE FROM dbms_stats._relation_stats_locked;
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', NULL);
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+
+-- No.14-6-8
+DELETE FROM dbms_stats._relation_stats_locked;
+VACUUM dbms_stats._relation_stats_locked;  -- in order to avoid auto vacuum
+SELECT dbms_stats.lock_database_stats();
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+BEGIN;
+SELECT * FROM internal_locks;
+SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
+SELECT * FROM internal_locks;
+COMMIT;
+SELECT starelid, attname, stainherit FROM columns_locked_v c;
+SELECT relid::regclass FROM dbms_stats._relation_stats_locked
+ GROUP BY relid
+ ORDER BY relid;