\pset null '(null)'
/*
 * No.2-3 dbms_stats.backup_history_id_seq
 */
-- No.2-3-1
SELECT setval('dbms_stats.backup_history_id_seq', 0, false);
ERROR:  setval: value 0 is out of bounds for sequence "backup_history_id_seq" (1..9223372036854775807)
-- No.2-3-2
SELECT setval('dbms_stats.backup_history_id_seq', 1, false);
 setval 
--------
      1
(1 row)

INSERT INTO dbms_stats.backup_history(time, unit)
    VALUES ('2011-01-01', 't');
SELECT id, time, unit FROM dbms_stats.backup_history
 ORDER BY id;
 id |             time             | unit 
----+------------------------------+------
  1 | Sat Jan 01 00:00:00 2011 PST | t
(1 row)

-- No.2-3-3
INSERT INTO dbms_stats.backup_history(time, unit)
    VALUES ('2011-01-02', 'c'), ('2011-01-03', 'd');
SELECT id, time, unit FROM dbms_stats.backup_history
 ORDER BY id;
 id |             time             | unit 
----+------------------------------+------
  1 | Sat Jan 01 00:00:00 2011 PST | t
  2 | Sun Jan 02 00:00:00 2011 PST | c
  3 | Mon Jan 03 00:00:00 2011 PST | d
(3 rows)

-- clean up
SELECT setval('dbms_stats.backup_history_id_seq', 1, false);
 setval 
--------
      1
(1 row)

DELETE FROM dbms_stats.backup_history;
/*
 * No.3-1 dbms_stats.use_locked_stats
 */
DELETE FROM dbms_stats.relation_stats_locked;
EXPLAIN (costs false) SELECT * FROM s0.st2 WHERE id < 1;
     QUERY PLAN     
--------------------
 Seq Scan on st2
   Filter: (id < 1)
(2 rows)

SELECT dbms_stats.lock_table_stats('s0.st2'::regclass);
 lock_table_stats 
------------------
 s0.st2
(1 row)

UPDATE dbms_stats.relation_stats_locked SET curpages = 10000;
VACUUM ANALYZE;
-- No.3-1-1
SET pg_dbms_stats.use_locked_stats TO ON;
EXPLAIN (costs false) SELECT * FROM s0.st2 WHERE id < 1;
           QUERY PLAN            
---------------------------------
 Index Scan using st2_idx on st2
   Index Cond: (id < 1)
(2 rows)

-- No.3-1-2
SET pg_dbms_stats.use_locked_stats TO OFF;
EXPLAIN (costs false) SELECT * FROM s0.st2 WHERE id < 1;
     QUERY PLAN     
--------------------
 Seq Scan on st2
   Filter: (id < 1)
(2 rows)

-- No.3-1-3
/* Reconnection as regular user */
\c - regular_user
SHOW pg_dbms_stats.use_locked_stats;
 pg_dbms_stats.use_locked_stats 
--------------------------------
 on
(1 row)

SET pg_dbms_stats.use_locked_stats TO OFF;
SHOW pg_dbms_stats.use_locked_stats;
 pg_dbms_stats.use_locked_stats 
--------------------------------
 off
(1 row)

EXPLAIN (costs false) SELECT * FROM s0.st2 WHERE id < 1;
     QUERY PLAN     
--------------------
 Seq Scan on st2
   Filter: (id < 1)
(2 rows)

RESET pg_dbms_stats.use_locked_stats;
EXPLAIN (costs false) SELECT * FROM s0.st2 WHERE id < 1;
           QUERY PLAN            
---------------------------------
 Index Scan using st2_idx on st2
   Index Cond: (id < 1)
(2 rows)

-- clean up
/* Reconnection as super user */
\c - super_user
DELETE FROM dbms_stats.relation_stats_locked;
/*
 * No.4-1 DATA TYPE dbms_stats.anyarray
 */
CREATE TABLE st3(id integer, name char(1000), num_arr char(5)[]);
INSERT INTO st3 SELECT i, i , ARRAY[i::char, 'a'] FROM generate_series(1,10) g(i);
ANALYZE st3;
SELECT staattnum, stavalues1 FROM pg_statistic
 WHERE starelid = 'public.st3'::regclass
 ORDER BY staattnum;
 staattnum |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   stavalues1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[encoded data, 10045 characters]
         1 | {1,2,3,4,5,6,7,8,9,10}
         2 | {"1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ","2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "}
         3 | {"{\"1    \",\"a    \"}"}
(3 rows)

\copy (SELECT stavalues1::dbms_stats.anyarray FROM dbms_stats.column_stats_effective WHERE starelid = 'st3'::regclass ORDER BY staattnum) TO 'results/anyarray_test.cp' binary
SET client_min_messages TO WARNING;
CREATE TABLE st4 (arr dbms_stats.anyarray, ord serial);
SET client_min_messages TO DEFAULT;
SELECT t.typname, n.nspname,
       t.typlen, t.typbyval, t.typtype,
       t.typcategory, t.typispreferred, t.typispreferred,
       t.typdelim, t.typrelid, t.typmodin,
       t.typmodout, t.typanalyze, t.typalign,
       t.typstorage, t.typnotnull, t.typbasetype, t.typtypmod,
       t.typndims, t.typcollation, t.typdefaultbin, t.typdefault
  FROM pg_type t, pg_namespace n
 WHERE t.typname = 'anyarray'
   AND t.typnamespace = n.oid
 ORDER BY t.typname;
 typname  |  nspname   | typlen | typbyval | typtype | typcategory | typispreferred | typispreferred | typdelim | typrelid | typmodin | typmodout | typanalyze | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault 
----------+------------+--------+----------+---------+-------------+----------------+----------------+----------+----------+----------+-----------+------------+----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------
 anyarray | pg_catalog |     -1 | f        | p       | P           | f              | f              | ,        |        0 | -        | -         | -          | d        | x          | f          |           0 |        -1 |        0 |            0 | (null)        | (null)
 anyarray | dbms_stats |     -1 | f        | b       | P           | f              | f              | ,        |        0 | -        | -         | -          | d        | x          | f          |           0 |        -1 |        0 |            0 | (null)        | (null)
(2 rows)

-- No.4-1-1
INSERT INTO st4 VALUES(NULL);
SELECT * FROM st4 ORDER BY ord;
  arr   | ord 
--------+-----
 (null) |   1
(1 row)

-- No.4-1-2
DELETE FROM st4;
SELECT stavalues1::dbms_stats.anyarray
  FROM dbms_stats.column_stats_effective
 WHERE starelid = 'st3'::regclass
   AND staattnum = 1;
       stavalues1       
------------------------
 {1,2,3,4,5,6,7,8,9,10}
(1 row)

SELECT count(*) FROM st4;
 count 
-------
     0
(1 row)

INSERT INTO st4
     SELECT stavalues1::dbms_stats.anyarray
       FROM dbms_stats.column_stats_effective
      WHERE starelid = 'st3'::regclass
        AND staattnum = 1;
SELECT * FROM st4 ORDER BY ord;
          arr           | ord 
------------------------+-----
 {1,2,3,4,5,6,7,8,9,10} |   2
(1 row)

-- No.4-1-3
DELETE FROM st4;
SELECT stavalues1::dbms_stats.anyarray
  FROM dbms_stats.column_stats_effective
 WHERE starelid = 'st3'::regclass
   AND staattnum = 2;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   stavalues1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[encoded data, 10033 characters]
 {"1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ","2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "}
(1 row)

SELECT count(*) FROM st4;
 count 
-------
     0
(1 row)

INSERT INTO st4
     SELECT stavalues1::dbms_stats.anyarray
       FROM dbms_stats.column_stats_effective
      WHERE starelid = 'st3'::regclass
        AND staattnum = 2;
SELECT * FROM st4 ORDER BY ord;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       arr                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | ord 
[encoded data, 10039 characters]
 {"1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ","2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "} |   3
(1 row)

-- No.4-1-4
DELETE FROM st4;
SELECT stavalues1::dbms_stats.anyarray
  FROM dbms_stats.column_stats_effective
 WHERE starelid = 'st3'::regclass
   AND staattnum = 1;
       stavalues1       
------------------------
 {1,2,3,4,5,6,7,8,9,10}
(1 row)

SELECT count(*) FROM st4;
 count 
-------
     0
(1 row)

INSERT INTO st4
     SELECT stavalues1::dbms_stats.anyarray
       FROM dbms_stats.column_stats_effective
      WHERE starelid = 'st3'::regclass
        AND staattnum = 1;
SELECT * FROM st4 ORDER BY ord;
          arr           | ord 
------------------------+-----
 {1,2,3,4,5,6,7,8,9,10} |   4
(1 row)

-- No.4-1-5
DELETE FROM st4;
SELECT stavalues1::dbms_stats.anyarray
  FROM dbms_stats.column_stats_effective
 WHERE starelid = 'st3'::regclass
   AND staattnum = 3;
        stavalues1         
---------------------------
 {"{\"1    \",\"a    \"}"}
(1 row)

SELECT count(*) FROM st4;
 count 
-------
     0
(1 row)

INSERT INTO st4
     SELECT stavalues1::dbms_stats.anyarray
       FROM dbms_stats.column_stats_effective
      WHERE starelid = 'st3'::regclass
        AND staattnum = 3;
SELECT * FROM st4 ORDER BY ord;
            arr            | ord 
---------------------------+-----
 {"{\"1    \",\"a    \"}"} |   5
(1 row)

-- No.4-1-6
DELETE FROM st4;
SELECT stavalues1::dbms_stats.anyarray
  FROM dbms_stats.column_stats_effective
 WHERE starelid = 'st3'::regclass
 ORDER BY staattnum;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   stavalues1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
[encoded data, 10033 characters]
 {1,2,3,4,5,6,7,8,9,10}
 {"1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ","2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "}
 {"{\"1    \",\"a    \"}"}
(3 rows)

\copy st4(arr) FROM 'results/anyarray_test.cp' binary
SELECT * FROM st4 ORDER BY ord;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       arr                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | ord 
[encoded data, 10039 characters]
 {1,2,3,4,5,6,7,8,9,10}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |   6
 {"1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ","2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","6                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","7                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","8                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ","9                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       "} |   7
 {"{\"1    \",\"a    \"}"}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |   8
(3 rows)

-- clean up
DROP TABLE st3;
DROP TABLE st4;
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
(0 rows)

SELECT dbms_stats.lock_table_stats('st1');
 lock_table_stats 
------------------
 st1
(1 row)

/*
 * No.5-2 invalid calls of dbms_stats.invalidate_column_cache
 */
-- No.5-2-1
SELECT dbms_stats.invalidate_column_cache();
ERROR:  pg_dbms_stats: invalid trigger call
-- No.5-2-2
/*
 * Driver function dbms_stats.invalidate_cache1
 */
CREATE TRIGGER invalidate_cache1
 AFTER INSERT OR DELETE OR UPDATE
    ON pt0
   FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_column_cache();
INSERT INTO pt0 VALUES (1,'2012/12/12');
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache1 ON pt0;
-- No.5-2-3
/*
 * Driver function dbms_stats.invalidate_cache2
 */
CREATE TRIGGER invalidate_cache2
BEFORE INSERT OR DELETE OR UPDATE
    ON pt0
   FOR EACH STATEMENT EXECUTE PROCEDURE dbms_stats.invalidate_column_cache();
INSERT INTO pt0 VALUES (1,'2012/12/12');
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache2 ON pt0;
-- No.5-2-4
/*
 * Driver function dbms_stats.invalidate_cache3
 */
CREATE TRIGGER invalidate_cache3
BEFORE TRUNCATE
    ON pt0
   FOR EACH STATEMENT EXECUTE PROCEDURE dbms_stats.invalidate_column_cache();
TRUNCATE TABLE pt0;
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache3 ON pt0;
-- No.5-2-5
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname) VALUES (0, 'dummy');
INSERT INTO dbms_stats.column_stats_locked (starelid, staattnum, stainherit)
    VALUES (0, 1, true);
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-2-6
INSERT INTO dbms_stats.column_stats_locked (starelid, staattnum, stainherit)
    VALUES ('st1_idx'::regclass, 1, true);
ERROR:  "st1_idx" is an index except an index expression
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-2-7
INSERT INTO dbms_stats.column_stats_locked (starelid, staattnum, stainherit)
    VALUES ('complex'::regclass, 1, true);
ERROR:  "complex" is a composite type
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-2-9
UPDATE dbms_stats.column_stats_locked SET stanullfrac = 1
 WHERE starelid = 'st1'::regclass
   AND staattnum = 1
   AND stainherit = false;
VACUUM ANALYZE;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

-- No.5-2-10
DELETE FROM dbms_stats.column_stats_locked
 WHERE starelid = 'st1'::regclass
   AND staattnum = 1
   AND stainherit = false;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-2-8
INSERT INTO dbms_stats.column_stats_locked
    (starelid, staattnum, stainherit, stanullfrac)
    VALUES ('st1'::regclass, 1, false, 1);
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

-- No.5-2-11
PREPARE p2 AS SELECT str FROM st1 WHERE lower(str) IS NULL;
EXPLAIN (costs false) SELECT str FROM st1 WHERE lower(str) IS NULL;
             QUERY PLAN             
------------------------------------
 Index Scan using st1_exp on st1
   Index Cond: (lower(str) IS NULL)
(2 rows)

EXPLAIN (costs false) EXECUTE p2;
             QUERY PLAN             
------------------------------------
 Index Scan using st1_exp on st1
   Index Cond: (lower(str) IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname)
    VALUES ('st1_exp'::regclass, 'dummy');
INSERT INTO dbms_stats.column_stats_locked
    (starelid, staattnum, stainherit, stanullfrac)
    VALUES ('st1_exp'::regclass, 1, false, 1);
EXPLAIN (costs false) SELECT str FROM st1 WHERE lower(str) IS NULL;
           QUERY PLAN           
--------------------------------
 Seq Scan on st1
   Filter: (lower(str) IS NULL)
(2 rows)

EXPLAIN (costs false) EXECUTE p2;
           QUERY PLAN           
--------------------------------
 Seq Scan on st1
   Filter: (lower(str) IS NULL)
(2 rows)

DEALLOCATE p2;
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 -
 st1
 st1_exp
(3 rows)

/*
 * No.5-3 dbms_stats.invalidate_relation_cache
 */
-- No.5-3-1
SELECT dbms_stats.invalidate_relation_cache();
ERROR:  pg_dbms_stats: invalid trigger call
-- No.5-3-2
/*
 * Driver function dbms_stats.invalidate_cache1
 */
CREATE TRIGGER invalidate_cache1
 AFTER INSERT OR DELETE OR UPDATE
    ON pt0
   FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache();
INSERT INTO pt0 VALUES (1,'2012/12/12');
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache1 ON pt0;
-- No.5-3-3
/*
 * Driver function dbms_stats.invalidate_cache2
 */
CREATE TRIGGER invalidate_cache2
BEFORE INSERT OR DELETE OR UPDATE
    ON pt0
   FOR EACH STATEMENT EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache();
INSERT INTO pt0 VALUES (1,'2012/12/12');
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache2 ON pt0;
-- No.5-3-4
/*
 * Driver function dbms_stats.invalidate_cache3
 */
CREATE TRIGGER invalidate_cache3
BEFORE TRUNCATE
    ON pt0
   FOR EACH STATEMENT EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache();
TRUNCATE TABLE pt0;
ERROR:  pg_dbms_stats: invalid trigger call
DROP TRIGGER invalidate_cache3 ON pt0;
-- No.5-3-5
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
(0 rows)

SELECT dbms_stats.lock_table_stats('st1');
 lock_table_stats 
------------------
 st1
(1 row)

EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname) VALUES (0, 'dummy');
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-3-6
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname)
    VALUES ('st1_idx'::regclass, 'st1_idx');
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-3-7
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname)
    VALUES ('complex'::regclass, 'complex');
ERROR:  "complex" is a composite type
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-3-9
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

UPDATE dbms_stats.relation_stats_locked SET curpages = 1
 WHERE relid = 'st1'::regclass;
VACUUM ANALYZE;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

-- No.5-3-10
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

DELETE FROM dbms_stats.relation_stats_locked
 WHERE relid = 'st1'::regclass;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-3-8
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

INSERT INTO dbms_stats.relation_stats_locked (relid, relname, curpages)
    VALUES ('st1'::regclass, 'st1', 1);
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

-- No.5-3-11
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 -
 st1
 st1_idx
(3 rows)

SELECT dbms_stats.lock_table_stats('st1');
 lock_table_stats 
------------------
 st1
(1 row)

SELECT relname, curpages FROM dbms_stats.relation_stats_locked
 WHERE relid = 'st1'::regclass;
  relname   | curpages 
------------+----------
 public.st1 |       45
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

VACUUM ANALYZE;
UPDATE dbms_stats.relation_stats_locked SET curpages = 1000
 WHERE relid = 'st1_exp'::regclass;
SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

SELECT relname, curpages FROM dbms_stats.relation_stats_locked
 WHERE relid = 'st1'::regclass;
  relname   | curpages 
------------+----------
 public.st1 |       45
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | f
(2 rows)

/*
 * No.5-4 StatsCacheRelCallback
 */
-- No.5-4-1
UPDATE dbms_stats.relation_stats_locked SET curpages = 1
 WHERE relid = 'st1'::regclass;
VACUUM ANALYZE;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

\c
SET pg_dbms_stats.use_locked_stats to NO;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

SET pg_dbms_stats.use_locked_stats to YES;
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 st1
(1 row)

SELECT dbms_stats.lock_table_stats('st1');
 lock_table_stats 
------------------
 st1
(1 row)

-- No.5-4-3
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

\! psql contrib_regression -c "UPDATE dbms_stats.column_stats_locked SET stanullfrac = 1 WHERE starelid = 'st1'::regclass"
UPDATE 2
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

-- No.5-4-4
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

\! psql contrib_regression -c "DELETE FROM dbms_stats.column_stats_locked WHERE starelid = 'st1'::regclass"
DELETE 2
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

-- No.5-4-2
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
           QUERY PLAN            
---------------------------------
 Index Scan using st1_idx on st1
   Index Cond: (val IS NULL)
(2 rows)

\! psql contrib_regression -c "INSERT INTO dbms_stats.column_stats_locked (starelid, staattnum, stainherit, stanullfrac) VALUES ('st1'::regclass, 1, false, 1)"
INSERT 0 1
EXPLAIN (costs false) SELECT * FROM st1 WHERE val IS NULL;
       QUERY PLAN        
-------------------------
 Seq Scan on st1
   Filter: (val IS NULL)
(2 rows)

SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 st1
(1 row)

-- No.5-4-5
CREATE TABLE s0.droptest(id integer);
INSERT INTO s0.droptest VALUES (1),(2),(3);
VACUUM ANALYZE;
SELECT * FROM s0.droptest
 WHERE id = 1;
 id 
----
  1
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

ALTER TABLE s0.droptest RENAME TO test;
SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | f
 column_stats_locked   | f
(2 rows)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

SELECT * FROM s0.test
 WHERE id = 1;
 id 
----
  1
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

ALTER TABLE s0.test RENAME TO droptest;
-- No.5-4-6
VACUUM ANALYZE;
SELECT * FROM s0.droptest
 WHERE id = 1;
 id 
----
  1
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

ALTER TABLE s0.droptest RENAME id TO test;
SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | f
 column_stats_locked   | f
(2 rows)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

SELECT * FROM s0.droptest
 WHERE test = 1;
 test 
------
    1
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

ALTER TABLE s0.droptest RENAME test TO id;
-- No.5-4-8
INSERT INTO s0.droptest VALUES (4);
SELECT * FROM s0.droptest
 WHERE id = 1;
 id 
----
  1
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

ANALYZE;
SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | f
 column_stats_locked   | f
(2 rows)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

SELECT * FROM s0.droptest
 WHERE id = 1;
 id 
----
  1
(1 row)

SELECT pg_sleep(1.0);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

-- No.5-4-9
DELETE FROM s0.droptest;
INSERT INTO s0.droptest VALUES (4),(5);
SELECT * FROM s0.droptest
 WHERE id = 4;
 id 
----
  4
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

VACUUM ANALYZE;
SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

SELECT reset_stat_and_cache();
 reset_stat_and_cache 
----------------------
 
(1 row)

SELECT * FROM s0.droptest
 WHERE id = 4;
 id 
----
  4
(1 row)

SELECT pg_sleep(0.7);
 pg_sleep 
----------
 
(1 row)

SELECT * FROM lockd_io;
        relname        | fetches 
-----------------------+---------
 relation_stats_locked | t
 column_stats_locked   | t
(2 rows)

-- clean up
DROP TABLE s0.droptest;
/*
 * No.6-1 dbms_stats.relname
 */
-- No.6-1-1
SELECT dbms_stats.relname('aaa', 'bbb');
 relname 
---------
 aaa.bbb
(1 row)

-- No.6-1-2
SELECT dbms_stats.relname(NULL, 'bbb');
 relname 
---------
 (null)
(1 row)

-- No.6-1-3
SELECT dbms_stats.relname('aaa', NULL);
 relname 
---------
 (null)
(1 row)

-- No.6-1-4
SELECT dbms_stats.relname(NULL, NULL);
 relname 
---------
 (null)
(1 row)

-- No.6-1-5
SELECT dbms_stats.relname('', '');
 relname 
---------
 "".""
(1 row)

-- No.6-1-6
SELECT dbms_stats.relname('aAa', 'bBb');
   relname   
-------------
 "aAa"."bBb"
(1 row)

-- No.6-1-7
SELECT dbms_stats.relname('a a', 'b b');
   relname   
-------------
 "a a"."b b"
(1 row)

-- No.6-1-8
SELECT dbms_stats.relname('a.a', 'b.b');
   relname   
-------------
 "a.a"."b.b"
(1 row)

-- No.6-1-9
SELECT dbms_stats.relname(E'a\na', E'b\nb');
 relname 
---------
 "a     +
 a"."b  +
 b"
(1 row)

-- No.6-1-10
SELECT dbms_stats.relname('a"a', 'b"b');
    relname    
---------------
 "a""a"."b""b"
(1 row)

-- No.6-1-11
SELECT dbms_stats.relname('あいう', '亞伊卯');
      relname      
-------------------
 "あいう"."亞伊卯"
(1 row)

/*
 * No.6-2 dbms_stats.is_system_schema
 */
-- No.6-2-1
SELECT dbms_stats.is_system_schema('pg_catalog');
 is_system_schema 
------------------
 t
(1 row)

-- No.6-2-2
SELECT dbms_stats.is_system_schema('pg_toast');
 is_system_schema 
------------------
 t
(1 row)

-- No.6-2-3
SELECT dbms_stats.is_system_schema('information_schema');
 is_system_schema 
------------------
 t
(1 row)

-- No.6-2-4
SELECT dbms_stats.is_system_schema('dbms_stats');
 is_system_schema 
------------------
 t
(1 row)

-- No.6-2-5
SELECT dbms_stats.is_system_schema(NULL);
 is_system_schema 
------------------
 (null)
(1 row)

-- No.6-2-6
SELECT dbms_stats.is_system_schema('');
 is_system_schema 
------------------
 f
(1 row)

-- No.6-2-7
SELECT dbms_stats.is_system_schema('s0');
 is_system_schema 
------------------
 f
(1 row)

-- No.6-2-8
/*
 * Driver function dbms_stats.is_system_schema1
 */
CREATE FUNCTION dbms_stats.is_system_schema1(schemaname text)
 RETURNS integer AS
 '$libdir/pg_dbms_stats', 'dbms_stats_is_system_schema'
 LANGUAGE C IMMUTABLE STRICT;
SELECT dbms_stats.is_system_schema1('s0');
 is_system_schema1 
-------------------
                 0
(1 row)

DROP FUNCTION dbms_stats.is_system_schema1(schemaname text);
/*
 * No.6-3 dbms_stats.is_system_catalog
 */
-- No.6-3-1
SELECT dbms_stats.is_system_catalog('s0.st0');
 is_system_catalog 
-------------------
 f
(1 row)

-- No.6-3-2
SELECT dbms_stats.is_system_catalog('st0');
 is_system_catalog 
-------------------
 f
(1 row)

-- No.6-3-3
SELECT dbms_stats.is_system_catalog('s00.s0');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.is_system_catalog('s00.s0');
                                            ^
-- No.6-3-4
SELECT dbms_stats.is_system_catalog(NULL);
 is_system_catalog 
-------------------
 t
(1 row)

-- No.6-3-5
/*
 * Driver function dbms_stats.is_system_catalog1
 */
CREATE FUNCTION dbms_stats.is_system_catalog1(relid regclass)
RETURNS integer AS
'$libdir/pg_dbms_stats', 'dbms_stats_is_system_catalog'
LANGUAGE C STABLE;
SELECT dbms_stats.is_system_catalog1('s0.st0');
 is_system_catalog1 
--------------------
                  0
(1 row)

DROP FUNCTION dbms_stats.is_system_catalog1(relid regclass);
/*
 * No.6-4 dbms_stats.is_target_relkind
 */
-- No.6-4-1
SELECT dbms_stats.is_target_relkind('r');
 is_target_relkind 
-------------------
 t
(1 row)

-- No.6-4-2
SELECT dbms_stats.is_target_relkind('i');
 is_target_relkind 
-------------------
 t
(1 row)

-- No.6-4-3
SELECT dbms_stats.is_target_relkind('S');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-4
SELECT dbms_stats.is_target_relkind('v');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-5
SELECT dbms_stats.is_target_relkind('c');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-6
SELECT dbms_stats.is_target_relkind('t');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-7
SELECT dbms_stats.is_target_relkind('a');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-8
SELECT dbms_stats.is_target_relkind('');
 is_target_relkind 
-------------------
 f
(1 row)

-- No.6-4-9
SELECT dbms_stats.is_target_relkind(NULL);
 is_target_relkind 
-------------------
 (null)
(1 row)

--#No.6-4-10 result varies according to a version
--#No.6-4-11 result varies according to a version
/*
 * No.7-1 dbms_stats.backup
 */
INSERT INTO dbms_stats.backup_history(id, time, unit) values(1, '2012-01-01', 'd');
-- No.7-1-1
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.st0'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid  
--------
 s0.st0
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 s0.st0   |         1
 s0.st0   |         2
(2 rows)

-- No.7-1-2
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 'st0'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
 st0
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
(2 rows)

-- No.7-1-3
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 'public.notfound'::regclass, NULL);
ERROR:  relation "public.notfound" does not exist
LINE 1: SELECT dbms_stats.backup(1, 'public.notfound'::regclass, NUL...
                                    ^
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.7-1-4
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.st0'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid  
--------
 s0.st0
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 s0.st0   |         1
 s0.st0   |         2
(2 rows)

-- No.7-1-5
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 'pg_toast.pg_toast_2618'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
(0 rows)

-- No.7-1-6
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.st0_idx'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 s0.st0_idx
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
(0 rows)

-- No.7-1-7
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.ss0'::regclass, NULL);
 backup 
--------
      1
(1 row)

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.7-1-8
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.sc0'::regclass, NULL);
 backup 
--------
      1
(1 row)

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.7-1-9 ut-<PG Version>
--#No.7-1-10 ut-<PG Version>
-- No.7-1-11
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.st0'::regclass, 1::int2);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid  
--------
 s0.st0
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 s0.st0   |         1
(1 row)

--#No.7-1-12 ut-<PG Version>
-- No.7-1-13
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 's0.st0'::regclass, NULL);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
 relid  
--------
 s0.st0
(1 row)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 s0.st0   |         1
 s0.st0   |         2
(2 rows)

--#No.7-1-14 ut-<PG Version>
-- No.7-1-15
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, 'pg_catalog.pg_class'::regclass, NULL);
 backup 
--------
      1
(1 row)

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.7-1-16
SELECT dbms_stats.backup(1, 's0.st0'::regclass, NULL);
 backup 
--------
      1
(1 row)

DELETE FROM dbms_stats.column_stats_backup;
SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
(0 rows)

SELECT count(*) FROM dbms_stats.relation_stats_backup;
 count 
-------
     1
(1 row)

-- SELECT dbms_stats.backup(1, 's0.st0'::regclass, NULL);
-- To avoid test unstability caused by relation id allocation, unique
-- constraint which used to be checked above is now checked more
-- directly in the following step.
SELECT ic.relname idxname, i.indisprimary
 FROM pg_index i
 JOIN pg_class c ON (c.oid = i.indrelid)
 JOIN pg_namespace n ON (n.oid = c.relnamespace)
 JOIN pg_class ic ON (ic.oid = i.indexrelid)
 WHERE n.nspname = 'dbms_stats' AND c.relname = 'relation_stats_backup';
          idxname           | indisprimary 
----------------------------+--------------
 relation_stats_backup_pkey | t
(1 row)

SELECT count(*) FROM dbms_stats.relation_stats_backup;
 count 
-------
     1
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_backup;
 count 
-------
     0
(1 row)

--#No.7-1-18 ut-<PG Version>
/*
 * 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;
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-2 dbms_stats.backup_database_stats
 */
SELECT setval('dbms_stats.backup_history_id_seq',8);
 setval 
--------
      8
(1 row)

-- No.8-2-1
SELECT dbms_stats.backup_database_stats('comment');
NOTICE:  arguments are <NULL>, <NULL>, comment
 backup_database_stats 
-----------------------
                     1
(1 row)

/*
 * No.8-4 dbms_stats.backup_table_stats(regclass,comment)
 */
-- No.8-4-1
SELECT dbms_stats.backup_table_stats('s0.st0', 'comment');
NOTICE:  arguments are s0.st0, <NULL>, comment
 backup_table_stats 
--------------------
                  1
(1 row)

-- No.8-4-2
SELECT dbms_stats.backup_table_stats('st0', 'comment');
NOTICE:  arguments are st0, <NULL>, comment
 backup_table_stats 
--------------------
                  1
(1 row)

-- No.8-4-3
SELECT dbms_stats.backup_table_stats('s00.s0', 'comment');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.backup_table_stats('s00.s0', 'comment');
                                             ^
/*
 * No.8-5 dbms_stats.backup_table_stats(schemaname, tablename, comment)
 */
-- No.8-5-1
SELECT dbms_stats.backup_table_stats('s0', 'st0', 'comment');
NOTICE:  arguments are s0.st0, <NULL>, comment
 backup_table_stats 
--------------------
                  1
(1 row)

-- No.8-5-2
SELECT dbms_stats.backup_table_stats('s00', 's0', 'comment');
ERROR:  schema "s00" does not exist
/*
 * No.8-6 dbms_stats.backup_column_stats(regclass, attname, comment)
 */
-- No.8-6-1
SELECT dbms_stats.backup_column_stats('s0.st0', 'id', 'comment');
NOTICE:  arguments are s0.st0, id, comment
 backup_column_stats 
---------------------
                   1
(1 row)

-- No.8-6-2
SELECT dbms_stats.backup_column_stats('st0', 'id', 'comment');
NOTICE:  arguments are st0, id, comment
 backup_column_stats 
---------------------
                   1
(1 row)

-- No.8-6-3
SELECT dbms_stats.backup_column_stats('s00.s0', 'id', 'comment');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.backup_column_stats('s00.s0', 'id', 'comme...
                                              ^
/*
 * No.8-7 dbms_stats.backup_column_stats(schemaname, tablename, attname, comment)
 */
-- No.8-7-1
SELECT dbms_stats.backup_column_stats('s0', 'st0', 'id', 'comment');
NOTICE:  arguments are s0.st0, id, comment
 backup_column_stats 
---------------------
                   1
(1 row)

-- No.8-7-2
SELECT dbms_stats.backup_column_stats('s00', 's0', 'id', 'comment');
ERROR:  schema "s00" does not exist
/*
 * 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)

VACUUM ANALYZE;
/*
 * Stab function dbms_stats.restore
 */
ALTER FUNCTION dbms_stats.restore(int8, regclass, text)
      RENAME TO truth_func_restore;
CREATE FUNCTION dbms_stats.restore(int8, regclass DEFAULT NULL, 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-3 dbms_stats.restore_table_stats(regclass, as_of_timestamp)
 */
-- No.10-3-1
SELECT dbms_stats.restore_table_stats('s0.st0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
 s0.st0
(1 row)

-- No.10-3-2
SELECT dbms_stats.restore_table_stats('s0.st0', '2012-02-29 23:59:57.000002');
NOTICE:  arguments are "3, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
 s0.st0
(1 row)

-- No.10-3-3
SELECT dbms_stats.restore_table_stats('s0.st0', '2012-01-01 00:00:00');
NOTICE:  arguments are "<NULL>, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
(0 rows)

--#No.10-3-4 is skipped after lock tests
-- No.10-3-5
SELECT dbms_stats.restore_table_stats('s0.st0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
 s0.st0
(1 row)

-- No.10-3-6
SELECT dbms_stats.restore_table_stats('st0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
 st0
(1 row)

-- No.10-3-7
SELECT dbms_stats.restore_table_stats('s00.s0', '2012-02-29 23:59:57');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.restore_table_stats('s00.s0', '2012-02-29 ...
                                              ^
/*
 * Stab dbms_stats.restore_table_stats(regclass, as_of_timestamp)
 */
ALTER FUNCTION dbms_stats.restore_table_stats(regclass,
											  timestamp with time zone)
      RENAME TO truth_func_restore_table_stats;
CREATE OR REPLACE FUNCTION dbms_stats.restore_table_stats(
    relid regclass,
    as_of_timestamp timestamp with time zone)
RETURNS SETOF regclass AS
$$
BEGIN
    RAISE NOTICE 'arguments are %, %', $1, $2;
    RETURN QUERY
        SELECT $1;
END
$$
LANGUAGE plpgsql;
/*
 * No.10-4 dbms_stats.restore_table_stats(schemaname, tablename, as_of_timestamp)
 */
-- No.10-4-1
SELECT dbms_stats.restore_table_stats('s0', 'st0', '2012-02-29 23:59:57');
NOTICE:  arguments are s0.st0, Wed Feb 29 23:59:57 2012 PST
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
 s0.st0
(1 row)

DROP FUNCTION dbms_stats.restore_table_stats(regclass,
											 timestamp with time zone);
ALTER FUNCTION dbms_stats.truth_func_restore_table_stats(regclass,
											  timestamp with time zone)
      RENAME TO restore_table_stats;
/*
 * No.10-5 dbms_stats.restore_column_stats(regclass, attname, as_of_timestamp)
 */
-- No.10-5-1
SELECT dbms_stats.restore_column_stats('s0.st0', 'id', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
 s0.st0
(1 row)

-- No.10-5-2
SELECT dbms_stats.restore_column_stats('s0.st0', 'id', '2012-02-29 23:59:57.000002');
NOTICE:  arguments are "3, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
 s0.st0
(1 row)

-- No.10-5-3
SELECT dbms_stats.restore_column_stats('s0.st0', 'id', '2012-01-01 00:00:00');
NOTICE:  arguments are "<NULL>, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
(0 rows)

--#No.10-5-4 is skipped after lock tests
-- No.10-5-5
SELECT dbms_stats.restore_column_stats('s0.st0', 'id', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
 s0.st0
(1 row)

-- No.10-5-6
SELECT dbms_stats.restore_column_stats('st0', 'id', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
 st0
(1 row)

-- No.10-5-7
SELECT dbms_stats.restore_column_stats('s00.s0', 'id', '2012-02-29 23:59:57');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.restore_column_stats('s00.s0', 'id', '2012...
                                               ^
/*
 * No.10-6 dbms_stats.restore_column_stats(
 *        schemaname, tablename, attname, as_of_timestamp)
 */
-- No.10-6-1
SELECT dbms_stats.restore_column_stats('s0', 'st0', 'id', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
 s0.st0
(1 row)

/*
 * No.15-1 dbms_stats.purge_stats
 */
-- No.15-1-1
SELECT * FROM dbms_stats.backup_history;
 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)

BEGIN;
SELECT relation::regclass, mode
  FROM pg_locks
  WHERE relation::regclass::text LIKE 'dbms_stats.\_%\_locked'
     OR relation::regclass::text LIKE 'dbms_stats.backup_history'
     OR relation::regclass::text LIKE 'dbms_stats.%\_backup'
  ORDER BY relation::regclass::text, mode;
 relation | mode 
----------+------
(0 rows)

SELECT id, unit, comment FROM dbms_stats.purge_stats(2);
 id | unit | comment 
----+------+---------
  1 | d    | (null)
  2 | d    | (null)
(2 rows)

SELECT relation::regclass, mode
  FROM pg_locks
  WHERE relation::regclass::text LIKE 'dbms_stats.\_%\_locked'
     OR relation::regclass::text LIKE 'dbms_stats.backup_history'
     OR relation::regclass::text LIKE 'dbms_stats.%\_backup'
  ORDER BY relation::regclass::text, mode;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.backup_history        | AccessShareLock
 dbms_stats.backup_history        | RowExclusiveLock
 dbms_stats.backup_history        | RowShareLock
 dbms_stats.column_stats_backup   | RowExclusiveLock
 dbms_stats.relation_stats_backup | RowExclusiveLock
(5 rows)

COMMIT;
SELECT * FROM dbms_stats.backup_history;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  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)
(6 rows)

-- No.15-1-6
SELECT id, unit, comment FROM dbms_stats.purge_stats(NULL);
ERROR:  backup id required
-- No.15-1-7
SELECT id, unit, comment FROM dbms_stats.purge_stats(-1);
ERROR:  backup id -1 not found
-- No.15-1-8
SELECT id, unit, comment FROM dbms_stats.purge_stats(2, NULL);
ERROR:  NULL is not allowed as the second parameter
-- No.15-1-4
SELECT * FROM dbms_stats.backup_history;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  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)
(6 rows)

SELECT id, unit, comment FROM dbms_stats.purge_stats(3);
 id | unit | comment 
----+------+---------
  3 | t    | (null)
(1 row)

SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

-- No.15-1-5
SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

SELECT id, unit, comment FROM dbms_stats.purge_stats(6);
WARNING:  no database-wide backup will remain after purge
HINT:  Give true for second parameter to purge forcibly.
 id | unit | comment 
----+------+---------
(0 rows)

SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

-- No.15-1-2
SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

SELECT id, unit, comment FROM dbms_stats.purge_stats(8);
WARNING:  no database-wide backup will remain after purge
HINT:  Give true for second parameter to purge forcibly.
 id | unit | comment 
----+------+---------
(0 rows)

SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

-- No.15-1-3
SELECT * FROM dbms_stats.backup_history;
 id |             time             | unit | comment 
----+------------------------------+------+---------
  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)
(5 rows)

SELECT id, unit, comment FROM dbms_stats.purge_stats(8, true);
 id | unit | comment 
----+------+---------
  4 | d    | (null)
  5 | c    | (null)
  6 | t    | (null)
  7 | t    | (null)
  8 | s    | (null)
(5 rows)

SELECT * FROM dbms_stats.backup_history;
 id | time | unit | comment 
----+------+------+---------
(0 rows)

/*
 * 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)

/*
 * restore test when only backup data does not exist 's0' schema
 */
DELETE FROM dbms_stats.column_stats_backup;
DELETE FROM dbms_stats.relation_stats_backup
 WHERE relname LIKE 's0.%';
SELECT count(*) FROM dbms_stats.relation_stats_backup;
 count 
-------
     9
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_backup;
 count 
-------
     0
(1 row)

-- No.10-2-8
SELECT dbms_stats.restore_schema_stats('s0', '2012-03-01 00:00:04');
 restore_schema_stats 
----------------------
(0 rows)

/*
 * restore test when there are only backup hisotory
 */
DELETE FROM dbms_stats.relation_stats_backup;
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.10-1-5
SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:58');
 restore_database_stats 
------------------------
(0 rows)

-- No.10-2-5
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:58');
 restore_schema_stats 
----------------------
(0 rows)

/*
 * restore when Backup does not exist
 */
DELETE FROM dbms_stats.backup_history;
SELECT count(*) FROM dbms_stats.backup_history;
 count 
-------
     0
(1 row)

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.10-1-4
SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
 restore_database_stats 
------------------------
(0 rows)

-- No.10-2-4
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
 restore_schema_stats 
----------------------
(0 rows)

-- No.10-3-4
SELECT dbms_stats.restore_table_stats('s0.st0', '2012-02-29 23:59:57');
NOTICE:  arguments are "<NULL>, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_table_stats" statement 1
 restore_table_stats 
---------------------
(0 rows)

-- No.10-5-4
SELECT dbms_stats.restore_column_stats('s0.st0', 'id', '2012-02-29 23:59:57');
NOTICE:  arguments are "<NULL>, s0.st0, id"
CONTEXT:  SQL function "restore_column_stats" statement 1
 restore_column_stats 
----------------------
(0 rows)

/*
 * Delete stab function dbms_stats.restore
 */
DROP FUNCTION dbms_stats.restore(int8, regclass, text);
ALTER FUNCTION dbms_stats.truth_func_restore(int8, regclass, text)
      RENAME TO restore;
/*
 * No.18-1 dbms_stats.clean_up_stats
 */
CREATE TABLE clean_test(id integer, num integer);
INSERT INTO clean_test SELECT i, i FROM generate_series(1,10) t(i);
ANALYZE clean_test;
-- No.18-1-1
-- No.18-1-5
SELECT dbms_stats.lock_table_stats('clean_test');
 lock_table_stats 
------------------
 clean_test
(1 row)

SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     1
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     2
(1 row)

SELECT dbms_stats.clean_up_stats();
 clean_up_stats 
----------------
(0 rows)

SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     1
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     2
(1 row)

-- No.18-1-2
-- No.18-1-7
DELETE FROM dbms_stats.relation_stats_locked;
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 dbms_stats.clean_up_stats();
 clean_up_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.18-1-3
SELECT dbms_stats.lock_table_stats('clean_test');
 lock_table_stats 
------------------
 clean_test
(1 row)

DROP TABLE clean_test;
SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     1
(1 row)

SELECT dbms_stats.clean_up_stats();
    clean_up_stats    
----------------------
 public.clean_test, 1
 public.clean_test, 2
 public.clean_test,
(3 rows)

SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     0
(1 row)

-- No.18-1-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     0
(1 row)

SELECT dbms_stats.clean_up_stats();
 clean_up_stats 
----------------
(0 rows)

SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     0
(1 row)

-- No.18-1-6
CREATE TABLE clean_test(id integer, num integer);
INSERT INTO clean_test SELECT i, i FROM generate_series(1,10) t(i);
ANALYZE clean_test;
SELECT dbms_stats.lock_table_stats('clean_test');
 lock_table_stats 
------------------
 clean_test
(1 row)

ALTER TABLE clean_test DROP COLUMN num;
ALTER TABLE clean_test ADD num integer;
UPDATE dbms_stats.column_stats_locked
   SET staattnum = 3
 WHERE starelid = 'clean_test'::regclass
   AND staattnum = 2;
UPDATE clean_test SET num = id;
SELECT count(*) FROM pg_statistic
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     2
(1 row)

SELECT dbms_stats.clean_up_stats();
 clean_up_stats 
----------------
(0 rows)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     2
(1 row)

-- No.18-1-8
DELETE FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass
   AND staattnum = 3;
SELECT count(*) FROM pg_statistic
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

SELECT dbms_stats.clean_up_stats();
 clean_up_stats 
----------------
(0 rows)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

-- No.18-1-9
ANALYZE clean_test;
SELECT dbms_stats.lock_table_stats('clean_test');
 lock_table_stats 
------------------
 clean_test
(1 row)

ALTER TABLE clean_test DROP COLUMN num;
SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     2
(1 row)

SELECT dbms_stats.clean_up_stats();
    clean_up_stats    
----------------------
 public.clean_test, 3
(1 row)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

-- No.18-1-10
DELETE FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass
   AND staattnum = 3;
SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

SELECT dbms_stats.clean_up_stats();
 clean_up_stats 
----------------
(0 rows)

SELECT count(*) FROM dbms_stats.column_stats_locked
 WHERE starelid = 'clean_test'::regclass;
 count 
-------
     1
(1 row)

DELETE FROM dbms_stats.relation_stats_locked;
DROP TABLE clean_test;
/*
 * No.19-1 dummy statistics view for general users privileges.
 */
SET SESSION AUTHORIZATION regular_user;
-- No.19-1-1
SELECT count(*) FROM dbms_stats.relation_stats_locked WHERE false;
ERROR:  permission denied for schema dbms_stats
LINE 1: SELECT count(*) FROM dbms_stats.relation_stats_locked WHERE ...
                             ^
-- No.19-1-2
SELECT count(*) FROM dbms_stats.column_stats_locked WHERE false;
ERROR:  permission denied for schema dbms_stats
LINE 1: SELECT count(*) FROM dbms_stats.column_stats_locked WHERE fa...
                             ^
-- No.19-1-3
SELECT count(*) FROM dbms_stats.stats WHERE false;
ERROR:  permission denied for schema dbms_stats
LINE 1: SELECT count(*) FROM dbms_stats.stats WHERE false;
                             ^
RESET SESSION AUTHORIZATION;
/*
 * No.20-1 confirm change at 1.3.5.
 */
SELECT CURRENT_USER;
 current_user 
--------------
 super_user
(1 row)

CREATE TABLE s0.st4 (a int, b text);
CREATE INDEX i_st4_a on s0.st4 (a);
CREATE VIEW s0.vst4 AS select * FROM s0.st4;
GRANT SELECT ON s0.vst4 TO regular_user;
INSERT INTO s0.st4 (SELECT a, a::text FROM generate_series(0, 999) a);
ANALYZE s0.st4;
SELECT dbms_stats.lock('s0.st4');
  lock  
--------
 s0.st4
(1 row)

DELETE FROM s0.st4;
INSERT INTO s0.st4 (SELECT 1, a::text FROM generate_series(0, 999) a);
ANALYZE s0.st4;
EXPLAIN (COSTS OFF) SELECT * FROM s0.vst4 WHERE a = 1;
           QUERY PLAN            
---------------------------------
 Index Scan using i_st4_a on st4
   Index Cond: (a = 1)
(2 rows)

EXPLAIN (COSTS OFF) SELECT * FROM s0.st4  WHERE a = 1;
           QUERY PLAN            
---------------------------------
 Index Scan using i_st4_a on st4
   Index Cond: (a = 1)
(2 rows)

SET SESSION AUTHORIZATION regular_user;
EXPLAIN (COSTS OFF) SELECT * FROM s0.st4  WHERE a = 1;
ERROR:  permission denied for relation st4
EXPLAIN (COSTS OFF) SELECT * FROM s0.vst4 WHERE a = 1;
           QUERY PLAN            
---------------------------------
 Index Scan using i_st4_a on st4
   Index Cond: (a = 1)
(2 rows)

SET pg_dbms_stats.use_locked_stats TO off;
EXPLAIN (COSTS OFF) SELECT * FROM s0.vst4 WHERE a = 1;
    QUERY PLAN     
-------------------
 Seq Scan on st4
   Filter: (a = 1)
(2 rows)

\c - super_user
ALTER TABLE dbms_stats.relation_stats_locked OWNER TO super_user;
SELECT dbms_stats.unlock('s0.st4');
 unlock 
--------
 s0.st4
(1 row)

DROP TABLE s0.st4 CASCADE;
NOTICE:  drop cascades to view s0.vst4
/*
 * No.21 anyarray stuffs
 */
CREATE TABLE st_ary (i int, f float, d timestamp without time zone);
INSERT INTO st_ary
 (SELECT a, random(), '2016-3-25 00:00:00'::date + (a || 'day')::interval
  FROM generate_series(0, 9999) a);
ANALYZE st_ary;
SELECT dbms_stats.lock('st_ary');
  lock  
--------
 st_ary
(1 row)

/* Identifying the base type of the target anyarray */
SELECT staattnum, dbms_stats.anyarray_basetype(stavalues1)
FROM dbms_stats.column_stats_locked
WHERE starelid = 'st_ary'::regclass
ORDER BY staattnum;
 staattnum | anyarray_basetype 
-----------+-------------------
         1 | int4
         2 | float8
         3 | timestamp
(3 rows)

/* Generating subsidiary functions and casts */
SELECT staattnum,
	    dbms_stats.prepare_statstweak(
			dbms_stats.anyarray_basetype(stavalues1)::regtype)
FROM dbms_stats.column_stats_locked
WHERE starelid = 'st_ary'::regclass
ORDER BY staattnum;
 staattnum |                                                                   prepare_statstweak                                                                    
-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------
         1 | (func dbms_stats._integer_ary_anyarray(integer[]), cast (integer[] AS dbms_stats.anyarray))
         2 | (func dbms_stats._double_precision_ary_anyarray(double precision[]), cast (double precision[] AS dbms_stats.anyarray))
         3 | (func dbms_stats._timestamp_without_time_zone_ary_anyarray(timestamp without time zone[]), cast (timestamp without time zone[] AS dbms_stats.anyarray))
(3 rows)

/* Tweaking stats */
UPDATE dbms_stats.column_stats_locked
SET stavalues1 = '{1,2,3,4,5}'::int[]
WHERE starelid = 'st_ary'::regclass AND staattnum = 1;
UPDATE dbms_stats.column_stats_locked
SET stavalues1 = '{1.1,2.2,3.3,4.4,5.5}'::float8[]
WHERE starelid = 'st_ary'::regclass AND staattnum = 2;
UPDATE dbms_stats.column_stats_locked
SET stavalues1 =
  (SELECT ARRAY(SELECT '2016-1-1 0:0:0'::timestamp without time zone +
                (i || 'day')::interval
                FROM generate_series(0, 10) i))
WHERE starelid = 'st_ary'::regclass AND staattnum = 3;
SELECT staattnum, stavalues1 FROM dbms_stats.column_stats_locked
WHERE starelid = 'st_ary'::regclass
ORDER BY staattnum;
 staattnum |                                                                                                                                                 stavalues1                                                                                                                                                 
-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
         1 | {1,2,3,4,5}
         2 | {1.1,2.2,3.3,4.4,5.5}
         3 | {"Fri Jan 01 00:00:00 2016","Sat Jan 02 00:00:00 2016","Sun Jan 03 00:00:00 2016","Mon Jan 04 00:00:00 2016","Tue Jan 05 00:00:00 2016","Wed Jan 06 00:00:00 2016","Thu Jan 07 00:00:00 2016","Fri Jan 08 00:00:00 2016","Sat Jan 09 00:00:00 2016","Sun Jan 10 00:00:00 2016","Mon Jan 11 00:00:00 2016"}
(3 rows)

/* Dropping tweak stuff */
SELECT proname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = 'dbms_stats' AND p.proname LIKE '\_%\_anyarray'
ORDER BY proname;
                  proname                  
-------------------------------------------
 _double_precision_ary_anyarray
 _integer_ary_anyarray
 _timestamp_without_time_zone_ary_anyarray
(3 rows)

SELECT staattnum,
	    dbms_stats.drop_statstweak(
			dbms_stats.anyarray_basetype(stavalues1)::regtype)
FROM dbms_stats.column_stats_locked
WHERE starelid = 'st_ary'::regclass
ORDER BY staattnum;
 staattnum |                                                                     drop_statstweak                                                                     
-----------+---------------------------------------------------------------------------------------------------------------------------------------------------------
         1 | (func dbms_stats._integer_ary_anyarray(integer[]), cast (integer[] AS dbms_stats.anyarray))
         2 | (func dbms_stats._double_precision_ary_anyarray(double precision[]), cast (double precision[] AS dbms_stats.anyarray))
         3 | (func dbms_stats._timestamp_without_time_zone_ary_anyarray(timestamp without time zone[]), cast (timestamp without time zone[] AS dbms_stats.anyarray))
(3 rows)

SELECT proname FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace
WHERE n.nspname = 'dbms_stats' AND p.proname LIKE '\_%\_anyarray'
ORDER BY proname;
 proname 
---------
(0 rows)

/* Immediately unlock for safety */
SELECT dbms_stats.unlock('st_ary');
 unlock 
--------
 st_ary
(1 row)

DROP TABLE st_ary;
