\pset null '(null)'
/*
 * No.2-1 table definitions.
 */
-- No.2-1-1
\d dbms_stats.backup_history
                                      Table "dbms_stats.backup_history"
 Column  |           Type           |                               Modifiers                                
---------+--------------------------+------------------------------------------------------------------------
 id      | bigint                   | not null default nextval('dbms_stats.backup_history_id_seq'::regclass)
 time    | timestamp with time zone | not null
 unit    | character(1)             | not null
 comment | text                     | 
Indexes:
    "backup_history_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE "dbms_stats.column_stats_backup" CONSTRAINT "column_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
    TABLE "dbms_stats.relation_stats_backup" CONSTRAINT "relation_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE

-- No.2-1-2
\d dbms_stats.column_stats_backup
    Table "dbms_stats.column_stats_backup"
   Column    |        Type         | Modifiers 
-------------+---------------------+-----------
 id          | bigint              | not null
 statypid    | oid                 | not null
 starelid    | oid                 | not null
 staattnum   | smallint            | not null
 stainherit  | boolean             | not null
 stanullfrac | real                | not null
 stawidth    | integer             | not null
 stadistinct | real                | not null
 stakind1    | smallint            | not null
 stakind2    | smallint            | not null
 stakind3    | smallint            | not null
 stakind4    | smallint            | not null
 stakind5    | smallint            | not null
 staop1      | oid                 | not null
 staop2      | oid                 | not null
 staop3      | oid                 | not null
 staop4      | oid                 | not null
 staop5      | oid                 | not null
 stanumbers1 | real[]              | 
 stanumbers2 | real[]              | 
 stanumbers3 | real[]              | 
 stanumbers4 | real[]              | 
 stanumbers5 | real[]              | 
 stavalues1  | dbms_stats.anyarray | 
 stavalues2  | dbms_stats.anyarray | 
 stavalues3  | dbms_stats.anyarray | 
 stavalues4  | dbms_stats.anyarray | 
 stavalues5  | dbms_stats.anyarray | 
Indexes:
    "column_stats_backup_pkey" PRIMARY KEY, btree (id, starelid, staattnum, stainherit)
Foreign-key constraints:
    "column_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
    "column_stats_backup_id_fkey1" FOREIGN KEY (id, starelid) REFERENCES dbms_stats.relation_stats_backup(id, relid) ON DELETE CASCADE

-- No.2-1-3
\d dbms_stats.column_stats_locked
    Table "dbms_stats.column_stats_locked"
   Column    |        Type         | Modifiers 
-------------+---------------------+-----------
 starelid    | oid                 | not null
 staattnum   | smallint            | not null
 stainherit  | boolean             | not null
 stanullfrac | real                | 
 stawidth    | integer             | 
 stadistinct | real                | 
 stakind1    | smallint            | 
 stakind2    | smallint            | 
 stakind3    | smallint            | 
 stakind4    | smallint            | 
 stakind5    | smallint            | 
 staop1      | oid                 | 
 staop2      | oid                 | 
 staop3      | oid                 | 
 staop4      | oid                 | 
 staop5      | oid                 | 
 stanumbers1 | real[]              | 
 stanumbers2 | real[]              | 
 stanumbers3 | real[]              | 
 stanumbers4 | real[]              | 
 stanumbers5 | real[]              | 
 stavalues1  | dbms_stats.anyarray | 
 stavalues2  | dbms_stats.anyarray | 
 stavalues3  | dbms_stats.anyarray | 
 stavalues4  | dbms_stats.anyarray | 
 stavalues5  | dbms_stats.anyarray | 
Indexes:
    "column_stats_locked_pkey" PRIMARY KEY, btree (starelid, staattnum, stainherit)
Foreign-key constraints:
    "column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats.relation_stats_locked(relid) ON DELETE CASCADE
Triggers:
    invalidate_column_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats.column_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_column_cache()

-- No.2-1-4
\d dbms_stats.relation_stats_backup
        Table "dbms_stats.relation_stats_backup"
      Column      |           Type           | Modifiers 
------------------+--------------------------+-----------
 id               | bigint                   | not null
 relid            | oid                      | not null
 relname          | text                     | not null
 relpages         | integer                  | not null
 reltuples        | real                     | not null
 relallvisible    | integer                  | not null
 curpages         | integer                  | not null
 last_analyze     | timestamp with time zone | 
 last_autoanalyze | timestamp with time zone | 
Indexes:
    "relation_stats_backup_pkey" PRIMARY KEY, btree (id, relid)
Foreign-key constraints:
    "relation_stats_backup_id_fkey" FOREIGN KEY (id) REFERENCES dbms_stats.backup_history(id) ON DELETE CASCADE
Referenced by:
    TABLE "dbms_stats.column_stats_backup" CONSTRAINT "column_stats_backup_id_fkey1" FOREIGN KEY (id, starelid) REFERENCES dbms_stats.relation_stats_backup(id, relid) ON DELETE CASCADE

-- No.2-1-5
\d dbms_stats.relation_stats_locked
        Table "dbms_stats.relation_stats_locked"
      Column      |           Type           | Modifiers 
------------------+--------------------------+-----------
 relid            | oid                      | not null
 relname          | text                     | not null
 relpages         | integer                  | 
 reltuples        | real                     | 
 relallvisible    | integer                  | 
 curpages         | integer                  | 
 last_analyze     | timestamp with time zone | 
 last_autoanalyze | timestamp with time zone | 
Indexes:
    "relation_stats_locked_pkey" PRIMARY KEY, btree (relid)
Referenced by:
    TABLE "dbms_stats.column_stats_locked" CONSTRAINT "column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats.relation_stats_locked(relid) ON DELETE CASCADE
Triggers:
    invalidate_relation_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats.relation_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache()

/*
 * No.2-2 view definitions.
 */
-- No.2-2-1
\dS+ dbms_stats.column_stats_effective
                View "dbms_stats.column_stats_effective"
   Column    |        Type         | Modifiers | Storage  | Description 
-------------+---------------------+-----------+----------+-------------
 starelid    | oid                 |           | plain    | 
 staattnum   | smallint            |           | plain    | 
 stainherit  | boolean             |           | plain    | 
 stanullfrac | real                |           | plain    | 
 stawidth    | integer             |           | plain    | 
 stadistinct | real                |           | plain    | 
 stakind1    | smallint            |           | plain    | 
 stakind2    | smallint            |           | plain    | 
 stakind3    | smallint            |           | plain    | 
 stakind4    | smallint            |           | plain    | 
 stakind5    | smallint            |           | plain    | 
 staop1      | oid                 |           | plain    | 
 staop2      | oid                 |           | plain    | 
 staop3      | oid                 |           | plain    | 
 staop4      | oid                 |           | plain    | 
 staop5      | oid                 |           | plain    | 
 stanumbers1 | real[]              |           | extended | 
 stanumbers2 | real[]              |           | extended | 
 stanumbers3 | real[]              |           | extended | 
 stanumbers4 | real[]              |           | extended | 
 stanumbers5 | real[]              |           | extended | 
 stavalues1  | dbms_stats.anyarray |           | extended | 
 stavalues2  | dbms_stats.anyarray |           | extended | 
 stavalues3  | dbms_stats.anyarray |           | extended | 
 stavalues4  | dbms_stats.anyarray |           | extended | 
 stavalues5  | dbms_stats.anyarray |           | extended | 
View definition:
 SELECT m.starelid,
    m.staattnum,
    m.stainherit,
    m.stanullfrac,
    m.stawidth,
    m.stadistinct,
    m.stakind1,
    m.stakind2,
    m.stakind3,
    m.stakind4,
    m.stakind5,
    m.staop1,
    m.staop2,
    m.staop3,
    m.staop4,
    m.staop5,
    m.stanumbers1,
    m.stanumbers2,
    m.stanumbers3,
    m.stanumbers4,
    m.stanumbers5,
    m.stavalues1,
    m.stavalues2,
    m.stavalues3,
    m.stavalues4,
    m.stavalues5
   FROM ( SELECT (dbms_stats.merge(v.*, s.*)).starelid AS starelid,
            (dbms_stats.merge(v.*, s.*)).staattnum AS staattnum,
            (dbms_stats.merge(v.*, s.*)).stainherit AS stainherit,
            (dbms_stats.merge(v.*, s.*)).stanullfrac AS stanullfrac,
            (dbms_stats.merge(v.*, s.*)).stawidth AS stawidth,
            (dbms_stats.merge(v.*, s.*)).stadistinct AS stadistinct,
            (dbms_stats.merge(v.*, s.*)).stakind1 AS stakind1,
            (dbms_stats.merge(v.*, s.*)).stakind2 AS stakind2,
            (dbms_stats.merge(v.*, s.*)).stakind3 AS stakind3,
            (dbms_stats.merge(v.*, s.*)).stakind4 AS stakind4,
            (dbms_stats.merge(v.*, s.*)).stakind5 AS stakind5,
            (dbms_stats.merge(v.*, s.*)).staop1 AS staop1,
            (dbms_stats.merge(v.*, s.*)).staop2 AS staop2,
            (dbms_stats.merge(v.*, s.*)).staop3 AS staop3,
            (dbms_stats.merge(v.*, s.*)).staop4 AS staop4,
            (dbms_stats.merge(v.*, s.*)).staop5 AS staop5,
            (dbms_stats.merge(v.*, s.*)).stanumbers1 AS stanumbers1,
            (dbms_stats.merge(v.*, s.*)).stanumbers2 AS stanumbers2,
            (dbms_stats.merge(v.*, s.*)).stanumbers3 AS stanumbers3,
            (dbms_stats.merge(v.*, s.*)).stanumbers4 AS stanumbers4,
            (dbms_stats.merge(v.*, s.*)).stanumbers5 AS stanumbers5,
            (dbms_stats.merge(v.*, s.*)).stavalues1 AS stavalues1,
            (dbms_stats.merge(v.*, s.*)).stavalues2 AS stavalues2,
            (dbms_stats.merge(v.*, s.*)).stavalues3 AS stavalues3,
            (dbms_stats.merge(v.*, s.*)).stavalues4 AS stavalues4,
            (dbms_stats.merge(v.*, s.*)).stavalues5 AS stavalues5
           FROM pg_statistic s
             FULL JOIN dbms_stats.column_stats_locked v USING (starelid, staattnum, stainherit)
          WHERE NOT dbms_stats.is_system_catalog(starelid::regclass) AND (EXISTS ( SELECT NULL::unknown
                   FROM pg_attribute a
                  WHERE a.attrelid = starelid AND a.attnum = staattnum AND a.attisdropped = false))) m
  WHERE m.starelid IS NOT NULL;

-- No.2-2-2
\dS+ dbms_stats.relation_stats_effective
                    View "dbms_stats.relation_stats_effective"
      Column      |           Type           | Modifiers | Storage  | Description 
------------------+--------------------------+-----------+----------+-------------
 relid            | oid                      |           | plain    | 
 relname          | text                     |           | extended | 
 relpages         | integer                  |           | plain    | 
 reltuples        | real                     |           | plain    | 
 relallvisible    | integer                  |           | plain    | 
 curpages         | integer                  |           | plain    | 
 last_analyze     | timestamp with time zone |           | plain    | 
 last_autoanalyze | timestamp with time zone |           | plain    | 
View definition:
 SELECT c.oid AS relid,
    dbms_stats.relname(n.nspname::text, c.relname::text) AS relname,
    COALESCE(v.relpages, c.relpages) AS relpages,
    COALESCE(v.reltuples, c.reltuples) AS reltuples,
    COALESCE(v.relallvisible, c.relallvisible) AS relallvisible,
    COALESCE(v.curpages, (pg_relation_size(c.oid::regclass) / current_setting('block_size'::text)::integer)::integer) AS curpages,
    COALESCE(v.last_analyze, pg_stat_get_last_analyze_time(c.oid)) AS last_analyze,
    COALESCE(v.last_autoanalyze, pg_stat_get_last_autoanalyze_time(c.oid)) AS last_autoanalyze
   FROM pg_class c
     JOIN pg_namespace n ON c.relnamespace = n.oid
     LEFT JOIN dbms_stats.relation_stats_locked v ON v.relid = c.oid
  WHERE dbms_stats.is_target_relkind(c.relkind) AND NOT dbms_stats.is_system_schema(n.nspname::text);

-- No.2-2-3
\dS+ dbms_stats.stats
                              View "dbms_stats.stats"
         Column         |        Type         | Modifiers | Storage  | Description 
------------------------+---------------------+-----------+----------+-------------
 schemaname             | name                |           | plain    | 
 tablename              | name                |           | plain    | 
 attname                | name                |           | plain    | 
 inherited              | boolean             |           | plain    | 
 null_frac              | real                |           | plain    | 
 avg_width              | integer             |           | plain    | 
 n_distinct             | real                |           | plain    | 
 most_common_vals       | dbms_stats.anyarray |           | extended | 
 most_common_freqs      | real[]              |           | extended | 
 histogram_bounds       | dbms_stats.anyarray |           | extended | 
 correlation            | real                |           | plain    | 
 most_common_elems      | dbms_stats.anyarray |           | extended | 
 most_common_elem_freqs | real[]              |           | extended | 
 elem_count_histogram   | real[]              |           | extended | 
View definition:
 SELECT n.nspname AS schemaname,
    c.relname AS tablename,
    a.attname,
    s.stainherit AS inherited,
    s.stanullfrac AS null_frac,
    s.stawidth AS avg_width,
    s.stadistinct AS n_distinct,
        CASE
            WHEN s.stakind1 = 1 THEN s.stavalues1
            WHEN s.stakind2 = 1 THEN s.stavalues2
            WHEN s.stakind3 = 1 THEN s.stavalues3
            WHEN s.stakind4 = 1 THEN s.stavalues4
            WHEN s.stakind5 = 1 THEN s.stavalues5
            ELSE NULL::dbms_stats.anyarray
        END AS most_common_vals,
        CASE
            WHEN s.stakind1 = 1 THEN s.stanumbers1
            WHEN s.stakind2 = 1 THEN s.stanumbers2
            WHEN s.stakind3 = 1 THEN s.stanumbers3
            WHEN s.stakind4 = 1 THEN s.stanumbers4
            WHEN s.stakind5 = 1 THEN s.stanumbers5
            ELSE NULL::real[]
        END AS most_common_freqs,
        CASE
            WHEN s.stakind1 = 2 THEN s.stavalues1
            WHEN s.stakind2 = 2 THEN s.stavalues2
            WHEN s.stakind3 = 2 THEN s.stavalues3
            WHEN s.stakind4 = 2 THEN s.stavalues4
            WHEN s.stakind5 = 2 THEN s.stavalues5
            ELSE NULL::dbms_stats.anyarray
        END AS histogram_bounds,
        CASE
            WHEN s.stakind1 = 3 THEN s.stanumbers1[1]
            WHEN s.stakind2 = 3 THEN s.stanumbers2[1]
            WHEN s.stakind3 = 3 THEN s.stanumbers3[1]
            WHEN s.stakind4 = 3 THEN s.stanumbers4[1]
            WHEN s.stakind5 = 3 THEN s.stanumbers5[1]
            ELSE NULL::real
        END AS correlation,
        CASE
            WHEN s.stakind1 = 4 THEN s.stavalues1
            WHEN s.stakind2 = 4 THEN s.stavalues2
            WHEN s.stakind3 = 4 THEN s.stavalues3
            WHEN s.stakind4 = 4 THEN s.stavalues4
            WHEN s.stakind5 = 4 THEN s.stavalues5
            ELSE NULL::dbms_stats.anyarray
        END AS most_common_elems,
        CASE
            WHEN s.stakind1 = 4 THEN s.stanumbers1
            WHEN s.stakind2 = 4 THEN s.stanumbers2
            WHEN s.stakind3 = 4 THEN s.stanumbers3
            WHEN s.stakind4 = 4 THEN s.stanumbers4
            WHEN s.stakind5 = 4 THEN s.stanumbers5
            ELSE NULL::real[]
        END AS most_common_elem_freqs,
        CASE
            WHEN s.stakind1 = 5 THEN s.stanumbers1
            WHEN s.stakind2 = 5 THEN s.stanumbers2
            WHEN s.stakind3 = 5 THEN s.stanumbers3
            WHEN s.stakind4 = 5 THEN s.stanumbers4
            WHEN s.stakind5 = 5 THEN s.stanumbers5
            ELSE NULL::real[]
        END AS elem_count_histogram
   FROM dbms_stats.column_stats_effective s
     JOIN pg_class c ON c.oid = s.starelid
     JOIN pg_attribute a ON c.oid = a.attrelid AND a.attnum = s.staattnum
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
  WHERE NOT a.attisdropped AND has_column_privilege(c.oid, a.attnum, 'select'::text);

-- No.2-2-4
\dS+ dbms_stats.column_stats_locked
                        Table "dbms_stats.column_stats_locked"
   Column    |        Type         | Modifiers | Storage  | Stats target | Description 
-------------+---------------------+-----------+----------+--------------+-------------
 starelid    | oid                 | not null  | plain    |              | 
 staattnum   | smallint            | not null  | plain    |              | 
 stainherit  | boolean             | not null  | plain    |              | 
 stanullfrac | real                |           | plain    |              | 
 stawidth    | integer             |           | plain    |              | 
 stadistinct | real                |           | plain    |              | 
 stakind1    | smallint            |           | plain    |              | 
 stakind2    | smallint            |           | plain    |              | 
 stakind3    | smallint            |           | plain    |              | 
 stakind4    | smallint            |           | plain    |              | 
 stakind5    | smallint            |           | plain    |              | 
 staop1      | oid                 |           | plain    |              | 
 staop2      | oid                 |           | plain    |              | 
 staop3      | oid                 |           | plain    |              | 
 staop4      | oid                 |           | plain    |              | 
 staop5      | oid                 |           | plain    |              | 
 stanumbers1 | real[]              |           | extended |              | 
 stanumbers2 | real[]              |           | extended |              | 
 stanumbers3 | real[]              |           | extended |              | 
 stanumbers4 | real[]              |           | extended |              | 
 stanumbers5 | real[]              |           | extended |              | 
 stavalues1  | dbms_stats.anyarray |           | extended |              | 
 stavalues2  | dbms_stats.anyarray |           | extended |              | 
 stavalues3  | dbms_stats.anyarray |           | extended |              | 
 stavalues4  | dbms_stats.anyarray |           | extended |              | 
 stavalues5  | dbms_stats.anyarray |           | extended |              | 
Indexes:
    "column_stats_locked_pkey" PRIMARY KEY, btree (starelid, staattnum, stainherit)
Foreign-key constraints:
    "column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats.relation_stats_locked(relid) ON DELETE CASCADE
Triggers:
    invalidate_column_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats.column_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_column_cache()

-- No.2-2-5
\dS+ dbms_stats.relation_stats_locked
                            Table "dbms_stats.relation_stats_locked"
      Column      |           Type           | Modifiers | Storage  | Stats target | Description 
------------------+--------------------------+-----------+----------+--------------+-------------
 relid            | oid                      | not null  | plain    |              | 
 relname          | text                     | not null  | extended |              | 
 relpages         | integer                  |           | plain    |              | 
 reltuples        | real                     |           | plain    |              | 
 relallvisible    | integer                  |           | plain    |              | 
 curpages         | integer                  |           | plain    |              | 
 last_analyze     | timestamp with time zone |           | plain    |              | 
 last_autoanalyze | timestamp with time zone |           | plain    |              | 
Indexes:
    "relation_stats_locked_pkey" PRIMARY KEY, btree (relid)
Referenced by:
    TABLE "dbms_stats.column_stats_locked" CONSTRAINT "column_stats_locked_starelid_fkey" FOREIGN KEY (starelid) REFERENCES dbms_stats.relation_stats_locked(relid) ON DELETE CASCADE
Triggers:
    invalidate_relation_cache BEFORE INSERT OR DELETE OR UPDATE ON dbms_stats.relation_stats_locked FOR EACH ROW EXECUTE PROCEDURE dbms_stats.invalidate_relation_cache()

/*
 * No.2-4 dbms_stats.anyarray
 */
-- No.2-4-1
SELECT n.nspname, t.typname, t.typlen, t.typbyval, t.typtype,
       t.typcategory, t.typispreferred, t.typisdefined, t.typdelim,
       t.typrelid, t.typelem, t.typinput, t.typoutput, t.typreceive,
       t.typsend, t.typmodin, t.typmodout, t.typanalyze, t.typalign,
       t.typstorage, t.typnotnull, t.typbasetype, t.typtypmod, t.typndims,
       t.typcollation, t.typdefaultbin, t.typdefault, t.typacl
  FROM pg_type t, pg_namespace n
 WHERE t.typnamespace = n.oid
   AND n.nspname = 'dbms_stats'
   AND t.typname = 'anyarray';
  nspname   | typname  | typlen | typbyval | typtype | typcategory | typispreferred | typisdefined | typdelim | typrelid | typelem |        typinput        |        typoutput        |        typreceive        |         typsend          | typmodin | typmodout | typanalyze | typalign | typstorage | typnotnull | typbasetype | typtypmod | typndims | typcollation | typdefaultbin | typdefault | typacl 
------------+----------+--------+----------+---------+-------------+----------------+--------------+----------+----------+---------+------------------------+-------------------------+--------------------------+--------------------------+----------+-----------+------------+----------+------------+------------+-------------+-----------+----------+--------------+---------------+------------+--------
 dbms_stats | anyarray |     -1 | f        | b       | P           | f              | t            | ,        |        0 |       0 | dbms_stats.anyarray_in | dbms_stats.anyarray_out | dbms_stats.anyarray_recv | dbms_stats.anyarray_send | -        | -         | -          | d        | x          | f          |           0 |        -1 |        0 |            0 | (null)        | (null)     | (null)
(1 row)

/*
 * No.5-1 dbms_stats.merge
 */
UPDATE pg_statistic SET
    stanullfrac = staattnum,
    stawidth = staattnum,
    stadistinct = staattnum,
    stakind1 = 4,
    stakind2 = 1,
    stakind3 = 2,
    stakind4 = 3,
    stakind5 = 5,
    staop1 = 14,
    staop2 = 11,
    staop3 = 12,
    staop4 = 13,
    staop5 = 15,
    stanumbers1 = ARRAY[staattnum,4],
    stanumbers2 = ARRAY[staattnum,1],
    stanumbers3 = ARRAY[staattnum,2],
    stanumbers4 = ARRAY[staattnum,3],
    stanumbers5 = ARRAY[staattnum,5],
    stavalues2 = array_cat(stavalues1,stavalues1),
    stavalues3 = array_cat(array_cat(stavalues1,stavalues1),stavalues1),
    stavalues4 = array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1)
   ,stavalues5 = array_cat(array_cat(array_cat(array_cat(stavalues1,stavalues1),stavalues1),stavalues1),stavalues1)
 WHERE starelid = 'st0'::regclass;
SELECT dbms_stats.lock_table_stats('st0');
 lock_table_stats 
------------------
 st0
(1 row)

UPDATE dbms_stats.column_stats_locked SET
    stainherit = 't',
    stanullfrac = -staattnum,
    stawidth = -staattnum,
    stadistinct = -staattnum,
    stakind1 = 2,
    stakind2 = 3,
    stakind3 = 4,
    stakind4 = 1,
    stakind5 = 5,
    staop1 = 22,
    staop2 = 23,
    staop3 = 24,
    staop4 = 21,
    staop5 = 25,
    stanumbers1 = ARRAY[-staattnum,22],
    stanumbers2 = ARRAY[-staattnum,23],
    stanumbers3 = ARRAY[-staattnum,24],
    stanumbers4 = ARRAY[-staattnum,21],
    stanumbers5 = ARRAY[-staattnum,25],
    stavalues1 = stavalues3,
    stavalues2 = stavalues2,
    stavalues3 = stavalues1,
    stavalues4 = stavalues4
   ,stavalues5 = stavalues5
;
/*
 * Driver function dbms_stats.merge1
 */
CREATE FUNCTION dbms_stats.merge1(
    lhs dbms_stats.column_stats_locked,
    rhs pg_catalog.pg_statistic
) RETURNS integer AS
'$libdir/pg_dbms_stats', 'dbms_stats_merge'
LANGUAGE C STABLE;
SELECT * FROM columns_locked_v
 WHERE starelid = 'st0'::regclass;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         1 | id      | t          |          -1 |       -1 |          -1 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-1,22}     | {-1,23}     | {-1,24}     | {-1,21}     | {-1,25}     | {1,2,1,2,1,2}             | {1,2,1,2}         | {1,2}      | {1,2,1,2,1,2,1,2}                 | {1,2,1,2,1,2,1,2,1,2}
 st0      |         2 | name    | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(2 rows)

SELECT * FROM plain_columns_statistic_v
 WHERE starelid = 'st0'::regclass;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 |    stavalues2     |        stavalues3         |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+-------------------+---------------------------+-----------------------------------+-------------------------------------------
 st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}         | {1,2,1,2,1,2}             | {1,2,1,2,1,2,1,2}                 | {1,2,1,2,1,2,1,2,1,2}
 st0      |         2 | f          |           2 |        2 |           2 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {2,4}       | {2,1}       | {2,2}       | {2,3}       | {2,5}       | {"test "}  | {"test ","test "} | {"test ","test ","test "} | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(2 rows)

SET client_min_messages TO LOG;
-- No.5-1-1
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(NULL, s)
         FROM pg_statistic s
        WHERE starelid = 'st0'::regclass
          AND staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
 st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
(1 row)

-- No.5-1-2
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(v, NULL)
         FROM dbms_stats.column_stats_locked v
        WHERE starelid = 'st0'::regclass
          AND staattnum = '2'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-3
SELECT dbms_stats.merge(NULL, NULL);
 merge  
--------
 (null)
(1 row)

-- No.5-1-4
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(v, s)
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-5
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(v, s)
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-6
SELECT dbms_stats.merge1(v, s)
  FROM dbms_stats.column_stats_locked v,
       pg_statistic s
 WHERE v.starelid = 'st0'::regclass
   AND v.staattnum = '2'::int2
   AND s.starelid = 'st0'::regclass
   AND s.staattnum = '1'::int2;
ERROR:  return type must be a row type
-- No.5-1-7
SELECT dbms_stats.merge(NULL, (
       s.starelid::regclass, s.staattnum, s.stainherit,
       s.stanullfrac, s.stawidth, s.stadistinct,
       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
       s.stakind5,
       s.staop1, s.staop2, s.staop3,
       s.staop4,
       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
       s.stanumbers5,
       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
      ,s.stavalues5
       ))
  FROM pg_statistic s
 WHERE s.starelid = 'st0'::regclass
   AND s.staattnum = '1'::int2;
 merge  
--------
 (null)
(1 row)

-- No.5-1-8
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(NULL, (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
              s.stakind5,
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM pg_statistic s
        WHERE s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
 st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | (null)      | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
(1 row)

-- No.5-1-9
SELECT dbms_stats.merge((
       v.starelid::regclass, v.staattnum, v.stainherit,
       v.stanullfrac, v.stawidth, v.stadistinct,
       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
       v.stakind5,
       v.staop1, v.staop2, v.staop3,
       v.staop4,
       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
       v.stanumbers5,
       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
      ,v.stavalues5
       ), NULL)
  FROM dbms_stats.column_stats_locked v
 WHERE v.starelid = 'st0'::regclass
   AND v.staattnum = '2'::int2;
 merge  
--------
 (null)
(1 row)

-- No.5-1-10
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((
              v.starelid::regclass, v.staattnum, v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
              v.stakind5,
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), NULL)
         FROM dbms_stats.column_stats_locked v
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | (null)      | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-11
SELECT dbms_stats.merge((
       v.starelid::regclass, v.staattnum, v.stainherit,
       v.stanullfrac, v.stawidth, v.stadistinct,
       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
       v.stakind5,
       v.staop1, v.staop2, v.staop3,
       v.staop4,
       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
       v.stanumbers5,
       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
      ,v.stavalues5
       ), (
       s.starelid::regclass, s.staattnum, s.stainherit,
       s.stanullfrac, s.stawidth, s.stadistinct,
       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
       s.stakind5,
       s.staop1, s.staop2, s.staop3,
       s.staop4,
       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
       s.stanumbers5,
       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
      ,s.stavalues5
       ))
  FROM dbms_stats.column_stats_locked v,
       pg_statistic s
 WHERE v.starelid = 'st0'::regclass
   AND v.staattnum = '2'::int2
   AND s.starelid = 'st0'::regclass
   AND s.staattnum = '1'::int2;
LOG:  pg_dbms_stats: bad statistics
DETAIL:  column "staop5" should not be null
 merge  
--------
 (null)
(1 row)

-- No.5-1-12
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((
              v.starelid::regclass, v.staattnum, v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
              v.stakind5,
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              NULL, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
              s.stakind5,
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              NULL, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | (null)      | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-13
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((
             NULL, NULL, NULL,
             NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL
            ,NULL, NULL, NULL, NULL
             ), s)
         FROM pg_statistic s
        WHERE s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
 st0      |         1 | f          |           1 |        1 |           1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
(1 row)

-- No.5-1-14
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(v, (
             NULL, NULL, NULL,
             NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL,
             NULL, NULL, NULL, NULL))
         FROM dbms_stats.column_stats_locked v
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-15
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge(v, s)
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        3 |        4 |        1 |        5 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-16
SELECT dbms_stats.merge((
       v.starelid::regclass, v.staattnum, v.stainherit,
       v.stanullfrac, v.stawidth, v.stadistinct,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL
      ,NULL, NULL, NULL, NULL
       ), (
       s.starelid::regclass, s.staattnum, s.stainherit,
       s.stanullfrac, s.stawidth, s.stadistinct,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL,
       NULL, NULL, NULL, NULL))
  FROM dbms_stats.column_stats_locked v,
       pg_statistic s
 WHERE v.starelid = 'st0'::regclass
   AND v.staattnum = '2'::int2
   AND s.starelid = 'st0'::regclass
   AND s.staattnum = '1'::int2;
LOG:  pg_dbms_stats: bad statistics
DETAIL:  column "stakind1" should not be null
 merge  
--------
 (null)
(1 row)

-- No.5-1-17
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((
              v.starelid::regclass, v.staattnum, v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              v.stakind1, v.stakind2, v.stakind3, v.stakind4,
              v.stakind5,
              v.staop1, v.staop2, v.staop3,
              v.staop4,
              NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              s.stakind1, s.stakind2, s.stakind3, s.stakind4,
              s.stakind5,
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '1'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 |  stavalues3   |    stavalues4     |      stavalues5       
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+---------------+-------------------+-----------------------
 st0      |         1 | t          |          -1 |       -1 |          -1 |        4 |        1 |        2 |        3 |        5 |     14 |     11 |     12 |     13 |     15 | {1,4}       | {1,1}       | {1,2}       | {1,3}       | {1,5}       | {1,2}      | {1,2,1,2}  | {1,2,1,2,1,2} | {1,2,1,2,1,2,1,2} | {1,2,1,2,1,2,1,2,1,2}
(1 row)

-- No.5-1-18
SELECT dbms_stats.merge((
       v.starelid::regclass, v.staattnum, v.stainherit,
       v.stanullfrac, v.stawidth, v.stadistinct,
       v.stakind1, v.stakind2, v.stakind3, v.stakind4,
       v.stakind5,
       v.staop1, v.staop2, v.staop3,
       v.staop4,
       NULL, v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
       v.stanumbers5,
       v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
      ,v.stavalues5
       ), (
       s.starelid::regclass, s.staattnum, s.stainherit,
       s.stanullfrac, s.stawidth, s.stadistinct,
       s.stakind1, s.stakind2, s.stakind3, s.stakind4,
       s.stakind5,
       s.staop1, s.staop2, s.staop3,
       s.staop4,
       NULL, s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
       s.stanumbers5,
       s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
      ,s.stavalues5
       ))
  FROM dbms_stats.column_stats_locked v,
       pg_statistic s
 WHERE v.starelid = 'st0'::regclass
   AND v.staattnum = '1'::int2
   AND s.starelid = 'st0'::regclass
   AND s.staattnum = '1'::int2;
LOG:  pg_dbms_stats: bad statistics
DETAIL:  column "staop5" should not be null
 merge  
--------
 (null)
(1 row)

-- No.5-1-19
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((
              v.starelid::regclass, v.staattnum, v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              '1', '1', '1', '1',
              '1',
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              '1', '1', '1', '1',
              '1',
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        1 |        1 |        1 |        1 |        1 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-20
SELECT (m.merge).starelid::regclass,
       (m.merge).staattnum,
       (m.merge).stainherit,
       (m.merge).stanullfrac,
       (m.merge).stawidth,
       (m.merge).stadistinct,
       (m.merge).stakind1,
       (m.merge).stakind2,
       (m.merge).stakind3,
       (m.merge).stakind4,
       (m.merge).stakind5,
       (m.merge).staop1,
       (m.merge).staop2,
       (m.merge).staop3,
       (m.merge).staop4,
       (m.merge).staop5,
       (m.merge).stanumbers1,
       (m.merge).stanumbers2,
       (m.merge).stanumbers3,
       (m.merge).stanumbers4,
       (m.merge).stanumbers5,
       (m.merge).stavalues1,
       (m.merge).stavalues2,
       (m.merge).stavalues3,
       (m.merge).stavalues4
      ,(m.merge).stavalues5
 FROM (SELECT dbms_stats.merge((v.starelid::regclass, v.staattnum, v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              '2', '2', '2', '2',
              '2',
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              '2', '2', '2', '2',
              '2',
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '2'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2) m;
 starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |        stavalues1         |    stavalues2     | stavalues3 |            stavalues4             |                stavalues5                 
----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+---------------------------+-------------------+------------+-----------------------------------+-------------------------------------------
 st0      |         2 | t          |          -2 |       -2 |          -2 |        2 |        2 |        2 |        2 |        2 |     22 |     23 |     24 |     21 |     25 | {-2,22}     | {-2,23}     | {-2,24}     | {-2,21}     | {-2,25}     | {"test ","test ","test "} | {"test ","test "} | {"test "}  | {"test ","test ","test ","test "} | {"test ","test ","test ","test ","test "}
(1 row)

-- No.5-1-21
SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              '1', '1', '1', '1',
              '1',
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              '1', '1', '1', '1',
              '1',
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '1'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2;
LOG:  pg_dbms_stats: bad column type
DETAIL:  type of column "name" has been changed
HINT:  need to execute dbms_stats.unlock('st0', 'name')
 merge  
--------
 (null)
(1 row)

-- No.5-1-22
SELECT dbms_stats.merge((v.starelid::regclass, '2', v.stainherit,
              v.stanullfrac, v.stawidth, v.stadistinct,
              '2', '2', '2', '2',
              '2',
              v.staop1, v.staop2, v.staop3, v.staop4,
              v.staop5,
              v.stanumbers1, v.stanumbers2, v.stanumbers3, v.stanumbers4,
              v.stanumbers5,
              v.stavalues1, v.stavalues2, v.stavalues3, v.stavalues4
             ,v.stavalues5
              ), (
              s.starelid::regclass, s.staattnum, s.stainherit,
              s.stanullfrac, s.stawidth, s.stadistinct,
              '2', '2', '2', '2',
              '2',
              s.staop1, s.staop2, s.staop3, s.staop4,
              s.staop5,
              s.stanumbers1, s.stanumbers2, s.stanumbers3, s.stanumbers4,
              s.stanumbers5,
              s.stavalues1, s.stavalues2, s.stavalues3, s.stavalues4
             ,s.stavalues5
              ))
         FROM dbms_stats.column_stats_locked v,
              pg_statistic s
        WHERE v.starelid = 'st0'::regclass
          AND v.staattnum = '1'::int2
          AND s.starelid = 'st0'::regclass
          AND s.staattnum = '1'::int2;
LOG:  pg_dbms_stats: bad column type
DETAIL:  type of column "name" has been changed
HINT:  need to execute dbms_stats.unlock('st0', 'name')
 merge  
--------
 (null)
(1 row)

RESET client_min_messages;
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 st0
(1 row)

/*
 * No.6-4 dbms_stats.is_target_relkind
 */
-- No.6-4- 10-11
SELECT dbms_stats.is_target_relkind(k::"char")
 FROM (VALUES ('r'), ('i'), ('f'), ('m'),
 			  ('S'), ('t'), ('v'), ('c')) t(k);
 is_target_relkind 
-------------------
 t
 t
 t
 t
 f
 f
 f
 f
(8 rows)

/*
 * No.7-1 dbms_stats.backup
 */
DELETE FROM dbms_stats.backup_history;
INSERT INTO dbms_stats.backup_history(id, time, unit) values(1, '2012-01-01', 'd');
-- No.7-1-9
SELECT dbms_stats.backup(1, 's0.sft0'::regclass, NULL);
 backup 
--------
      1
(1 row)

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

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

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

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

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

-- No.7-1-12
DELETE FROM dbms_stats.relation_stats_backup;
SELECT dbms_stats.backup(1, NULL, 1::int2);
 backup 
--------
      1
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s1.st0
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
(16 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st1      |         1
 s0.st0   |         1
 s0.st1   |         1
 s0.st2   |         1
 s1.st0   |         1
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
(9 rows)

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

SELECT relid::regclass FROM dbms_stats.relation_stats_backup
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s1.st0
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
(16 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_backup
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
 st1      |         1
 st1      |         2
 s0.st0   |         1
 s0.st0   |         2
 s0.st1   |         1
 s0.st1   |         2
 s0.st2   |         1
 s0.st2   |         2
 s1.st0   |         1
 s1.st0   |         2
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
 s0.smv0  |         2
 s0.smv0  |         3
(17 rows)

-- No.7-1-18
DELETE FROM dbms_stats.relation_stats_backup;
\! psql contrib_regression -c "SELECT dbms_stats.backup(NULL, 's0.st0'::regclass, NULL)" > results/ut_no2_1_17.out 2>&1
SELECT count(*) FROM dbms_stats.relation_stats_backup;
 count 
-------
     0
(1 row)

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

/*
 * No.8-1 dbms_stats.backup
 */
SELECT setval('dbms_stats.backup_history_id_seq',1, false);
 setval 
--------
      1
(1 row)

/*
 * Stab function dbms_stats.backup
 */
ALTER FUNCTION dbms_stats.backup(
    backup_id int8,
    relid regclass,
    attnum int2)
    RENAME TO truth_func_backup;
CREATE OR REPLACE FUNCTION dbms_stats.backup(
    backup_id int8,
    regclass,
    attnum int2)
RETURNS int8 AS
$$
BEGIN
    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
    RETURN 1;
END;
$$
LANGUAGE plpgsql;
-- No.8-1-1
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
NOTICE:  arguments are 1, s0.st0, 1
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  1 | c    | dummy comment
(1 row)

-- No.8-1-2
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 2, s0.st0, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  2 | t    | dummy comment
(1 row)

-- No.8-1-3
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup(NULL::regclass, 'id', 'dummy comment');
ERROR:  relation required
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-4
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup(NULL::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 3, <NULL>, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  3 | d    | dummy comment
(1 row)

-- No.8-1-5
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup(0, NULL, 'dummy comment');
ERROR:  relation "-" not found
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-6
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.st0'::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 4, s0.st0, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  4 | t    | dummy comment
(1 row)

-- No.8-1-7
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup(
    'pg_toast.pg_toast_2618'::regclass,
    NULL,
    'dummy comment');
ERROR:  relation of relkind "t" cannot have statistics to backup: "pg_toast.pg_toast_2618"
HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-8
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.st0_idx'::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 5, s0.st0_idx, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  5 | t    | dummy comment
(1 row)

-- No.8-1-9
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.ss0'::regclass, NULL, 'dummy comment');
ERROR:  relation of relkind "S" cannot have statistics to backup: "s0.ss0"
HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-10
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.sc0'::regclass, NULL, 'dummy comment');
ERROR:  relation of relkind "c" cannot have statistics to backup: "s0.sc0"
HINT:  Only tables(r), materialized views(m), foreign tables(f) and indexes(i) are allowed.
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-11
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.sft0'::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 6, s0.sft0, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  6 | t    | dummy comment
(1 row)

-- No.8-1-12
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.smv0'::regclass, NULL, 'dummy comment');
NOTICE:  arguments are 7, s0.smv0, <NULL>
CONTEXT:  SQL statement "INSERT INTO dbms_stats.backup_history(time, unit, comment)
        VALUES (current_timestamp, unit_type, $3)
        RETURNING dbms_stats.backup(id, $1, set_attnum)"
PL/pgSQL function dbms_stats.backup(regclass,text,text) line 42 at SQL statement
 backup 
--------
      1
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit |    comment    
----+------+---------------
  7 | t    | dummy comment
(1 row)

-- No.8-1-13
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('pg_catalog.pg_class'::regclass, NULL, 'dummy comment');
ERROR:  backing up statistics is inhibited for system catalogs: "pg_class"
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-14
DELETE FROM dbms_stats.backup_history;
SELECT dbms_stats.backup('s0.st0'::regclass, 'dummy', 'dummy comment');
ERROR:  column "dummy" not found in relation "s0.st0"
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

-- No.8-1-15
DELETE FROM dbms_stats.backup_history;
DELETE FROM pg_statistic
 WHERE starelid = 's0.st0'::regclass
   AND staattnum = 1::int2;
SELECT count(*) FROM dbms_stats.column_stats_effective
 WHERE starelid = 's0.st0'::regclass
   AND staattnum = 1::int2;
 count 
-------
     0
(1 row)

SELECT dbms_stats.backup('s0.st0'::regclass, 'id', 'dummy comment');
ERROR:  no statistics available for column "id" of relation "s0.st0"
SELECT id, unit, comment FROM dbms_stats.backup_history;
 id | unit | comment 
----+------+---------
(0 rows)

/*
 * Stab function dbms_stats.backup
 */
ALTER FUNCTION dbms_stats.backup(
    relid regclass,
    attname text,
    comment text)
    RENAME TO truth_func_backup;
CREATE OR REPLACE FUNCTION dbms_stats.backup(
    relid regclass DEFAULT NULL,
    attname text DEFAULT NULL,
    comment text DEFAULT NULL)
RETURNS int8 AS
$$
BEGIN
    IF $3 = '<NULL>' THEN
        RAISE NOTICE 'third argument is not NULL but string "<NULL>"';
    END IF;
    RAISE NOTICE 'arguments are %, %, %', $1, $2, $3;
    RETURN 1;
END;
$$
LANGUAGE plpgsql;
/*
 * No.8-3 dbms_stats.backup_schema_stats
 */
SELECT setval('dbms_stats.backup_history_id_seq',9, false);
 setval 
--------
      9
(1 row)

-- No.8-3-1
SELECT dbms_stats.backup_schema_stats('s0', 'comment');
NOTICE:  arguments are 9, s0.st0, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.st0_idx, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.st1, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.st1_idx, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.st2, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.st2_idx, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.sft0, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
NOTICE:  arguments are 9, s0.smv0, <NULL>
CONTEXT:  SQL statement "SELECT dbms_stats.backup(backup_id, cn.oid, NULL)
      FROM (SELECT c.oid
              FROM pg_catalog.pg_class c,
                   pg_catalog.pg_namespace n
             WHERE n.nspname = schemaname
               AND c.relnamespace = n.oid
               AND dbms_stats.is_target_relkind(c.relkind)
             ORDER BY c.oid
           ) cn"
PL/pgSQL function dbms_stats.backup_schema_stats(text,text) line 17 at PERFORM
 backup_schema_stats 
---------------------
                   9
(1 row)

SELECT id, unit, comment FROM dbms_stats.backup_history
 ORDER BY id DESC
 LIMIT 1;
 id | unit | comment 
----+------+---------
  9 | s    | comment
(1 row)

-- No.8-3-2
SELECT dbms_stats.backup_schema_stats('s00', 'comment');
ERROR:  schema "s00" not found
SELECT id, unit, comment FROM dbms_stats.backup_history
 ORDER BY id DESC
 LIMIT 1;
 id | unit | comment 
----+------+---------
  9 | s    | comment
(1 row)

-- No.8-3-3
SELECT dbms_stats.backup_schema_stats('pg_catalog', 'comment');
ERROR:  backing up statistics is inhibited for system schemas: "pg_catalog"
SELECT id, unit, comment FROM dbms_stats.backup_history
 ORDER BY id DESC
 LIMIT 1;
 id | unit | comment 
----+------+---------
  9 | s    | comment
(1 row)

/*
 * Delete stab function dbms_stats.backup
 */
DROP FUNCTION dbms_stats.backup(
    backup_id int8,
    regclass,
    attnum int2);
ALTER FUNCTION dbms_stats.truth_func_backup(
    backup_id int8,
    regclass,
    attnum int2)
    RENAME TO backup;
DROP FUNCTION dbms_stats.backup(
    regclass,
    attname text,
    comment text);
ALTER FUNCTION dbms_stats.truth_func_backup(
    regclass,
    attname text,
    comment text)
    RENAME TO backup;
VACUUM ANALYZE;
/*
 * create backup statistics state A
 */
DELETE FROM dbms_stats.backup_history;
INSERT INTO dbms_stats.backup_history(id, time, unit)
    VALUES (1, '2012-02-29 23:59:56.999999', 'd');
SELECT setval('dbms_stats.backup_history_id_seq',1);
 setval 
--------
      1
(1 row)

SELECT dbms_stats.backup();
 backup 
--------
      2
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-02-29 23:59:57'
 WHERE id = 2;
SELECT dbms_stats.backup('s0.st0');
 backup 
--------
      3
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-02-29 23:59:57.000001'
 WHERE id = 3;
SELECT dbms_stats.backup();
 backup 
--------
      4
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-02-29 23:59:58'
 WHERE id = 4;
DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 4;
SELECT dbms_stats.backup('s0.st0', 'id');
 backup 
--------
      5
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-03-01 00:00:00'
 WHERE id = 5;
SELECT dbms_stats.backup('s0.st0');
 backup 
--------
      6
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-03-01 00:00:02'
 WHERE id = 6;
SELECT dbms_stats.backup('public.st0');
 backup 
--------
      7
(1 row)

UPDATE dbms_stats.backup_history
   SET time = '2012-03-01 00:00:04'
 WHERE id = 7;
INSERT INTO dbms_stats.backup_history(time, unit)
    VALUES ('2012-03-01 00:00:06', 's');
SELECT dbms_stats.backup(8, c.oid, NULL)
  FROM pg_catalog.pg_class c,
       pg_catalog.pg_namespace n
 WHERE n.nspname = 's0'
   AND c.relnamespace = n.oid
   AND c.relkind IN ('r', 'i');
 backup 
--------
      8
      8
      8
      8
      8
      8
(6 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

SELECT * FROM relations_backup_v;
 id |   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
----+------------+----------------+----------+-----------+---------------+----------
  2 | pt0        | public.pt0     |        0 |         0 |             0 |        0
  2 | pt0_idx    | public.pt0_idx |        2 |         0 |             0 |        2
  2 | st0        | public.st0     |        1 |         2 |             1 |        1
  2 | st0_idx    | public.st0_idx |        2 |         2 |             0 |        2
  2 | st1        | public.st1     |       45 |     10000 |            45 |       45
  2 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
  2 | s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
  2 | s0.st1     | s0.st1         |        1 |         3 |             1 |        1
  2 | s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
  2 | s0.st2     | s0.st2         |        1 |         3 |             1 |        1
  2 | s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
  2 | s1.st0     | s1.st0         |        1 |         4 |             1 |        1
  2 | st1_idx    | public.st1_idx |       30 |     10000 |             0 |       30
  2 | st1_exp    | public.st1_exp |       30 |     10000 |             0 |       30
  2 | s0.sft0    | s0.sft0        |        1 |        10 |             0 |        0
  2 | s0.smv0    | s0.smv0        |        1 |         2 |             1 |        1
  3 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
  5 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
  6 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
  7 | st0        | public.st0     |        1 |         2 |             1 |        1
  8 | s0.st0     | s0.st0         |        1 |         2 |             1 |        1
  8 | s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
  8 | s0.st1     | s0.st1         |        1 |         3 |             1 |        1
  8 | s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
  8 | s0.st2     | s0.st2         |        1 |         3 |             1 |        1
  8 | s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
(26 rows)

SELECT * FROM columns_backup_v;
 id | statypid | starelid | staattnum | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 |      stanumbers1       | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----+----------+----------+-----------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+------------------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
  2 |       23 | st0      |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  2 |     1042 | st0      |         2 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}                    | {1}         | (null)      | (null)      | (null)      | {"test "}              | (null)     | (null)     | (null)     | (null)
  2 |       23 | st1      |         1 | f          |           0 |        4 |           3 |        1 |        3 |        0 |        0 |        0 |     96 |     97 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
  2 |       25 | st1      |         2 | f          |           0 |        2 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st1   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st1   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.st2   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
  2 |       25 | s0.st2   |         2 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
  2 |       23 | s1.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4}              | (null)     | (null)     | (null)     | (null)
  2 |       23 | s1.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {15,25,35,45}          | (null)     | (null)     | (null)     | (null)
  2 |       25 | st1_exp  |         1 | f          |           0 |        5 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}                | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.sft0  |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.smv0  |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  2 |       23 | s0.smv0  |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
  2 |       25 | s0.smv0  |         3 | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,test}               | (null)     | (null)     | (null)     | (null)
  3 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  3 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
  3 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
  3 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
  5 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  5 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
  6 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  6 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
  6 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
  6 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
  7 |       23 | st0      |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  7 |     1042 | st0      |         2 | f          |           0 |        6 |        -0.5 |        1 |        3 |        0 |        0 |        0 |   1054 |   1058 |      0 |      0 |      0 | {1}                    | {1}         | (null)      | (null)      | (null)      | {"test "}              | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st0   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2}                  | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st0   |         1 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}            | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st0   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20}                | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st0   |         2 | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60}       | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st1   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {4,5,6}                | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st1   |         2 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {40,50,60}             | (null)     | (null)     | (null)     | (null)
  8 |       23 | s0.st2   |         1 | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)                 | {1}         | (null)      | (null)      | (null)      | {1,2,3}                | (null)     | (null)     | (null)     | (null)
  8 |       25 | s0.st2   |         2 | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)                 | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test}       | (null)     | (null)     | (null)     | (null)
(39 rows)

VACUUM ANALYZE;
/*
 * No.9-1 dbms_stats.restore
 */
-- No.9-1-1
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.restore(2, 's0.st0', NULL);
 restore 
---------
 s0.st0
(1 row)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.backup_history        | RowShareLock
 dbms_stats.column_stats_backup   | AccessShareLock
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_backup | AccessShareLock
 dbms_stats.relation_stats_backup | RowShareLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(7 rows)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
(4 rows)

-- No.9-1-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 'st0', NULL);
 restore 
---------
 st0
(1 row)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
(2 rows)

-- No.9-1-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's00.s0', NULL);
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.restore(2, 's00.s0', NULL);
                                     ^
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(NULL, 's0.st0', NULL);
ERROR:  backup id required
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's0.st0', 'id');
 restore 
---------
 s0.st0
(1 row)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 s0.st0   | id      | f
 s0.st0   | id      | t
(2 rows)

-- No.9-1-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, NULL, 'id');
ERROR:  relation required
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's0.st0', NULL);
 restore 
---------
 s0.st0
(1 row)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
(4 rows)

-- No.9-1-8
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, NULL, NULL);
  restore   
------------
 pt0
 pt0_idx
 s0.sft0
 s0.smv0
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s1.st0
 st0
 st0_idx
 st1
 st1_exp
 st1_idx
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s1.st0
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
(19 rows)

-- No.9-1-9
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(0, 's0.st0', NULL);
ERROR:  backup id 0 not found
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
(0 rows)

-- No.9-1-10
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 0, 'id');
ERROR:  relation "-" not found
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-11
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(1, 's0.st0', NULL);
ERROR:  statistics of relation "s0.st0" not found in any backups before backup id = 1
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-12
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's0.st0', 'dummy');
ERROR:  column "dummy" not found in relation s0.st0
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-13
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(1, 's0.st0', 'id');
ERROR:  statistics of relation "s0.st0" not found in any backups before backup id = 1
SELECT count(*) FROM dbms_stats.column_stats_locked;
 count 
-------
     0
(1 row)

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

-- No.9-1-15
DELETE FROM dbms_stats.relation_stats_locked;
ALTER TABLE s1.st0 DROP COLUMN id;
SELECT dbms_stats.restore(2, 's1.st0', 'id');
ERROR:  column "id" not found in relation s1.st0
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
(0 rows)

-- No.9-1-14
DELETE FROM dbms_stats.relation_stats_locked;
\set s1_st0_oid `psql contrib_regression -tA -c "SELECT c.oid FROM pg_class c, pg_namespace n WHERE c.relnamespace = n.oid AND n.nspname = 's1' AND c.relname = 'st0';"`
DROP TABLE s1.st0;
-- SELECT dbms_stats.restore(2, :s1_st0_oid, NULL);
-- To avoid test unstability caused by relation id alloction, the test
-- above is omitted.
SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
     0
(1 row)

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

CREATE TABLE s1.st0(id integer, num integer);
INSERT INTO s1.st0 VALUES (1, 15), (2, 25), (3, 35), (4, 45);
VACUUM ANALYZE;
-- No.9-1-16
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's0.st0', NULL);
 restore 
---------
 s0.st0
(1 row)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
(4 rows)

-- No.9-1-17
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (2,
             'pg_toast.pg_toast_2618'::regclass,
             'pg_toast.pg_toast_2618', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 2
   AND relname = 'pg_toast.pg_toast_2618';
 id |         relid          |        relname         | relpages | reltuples | relallvisible | curpages 
----+------------------------+------------------------+----------+-----------+---------------+----------
  2 | pg_toast.pg_toast_2618 | pg_toast.pg_toast_2618 |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 'pg_toast.pg_toast_2618', NULL);
 restore 
---------
(0 rows)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 2
   AND relname = 'pg_toast.pg_toast_2618';
-- No.9-1-18
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore(2, 's0.st0_idx', NULL);
  restore   
------------
 s0.st0_idx
(1 row)

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

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
(0 rows)

-- No.9-1-19
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (2, 's0.ss0'::regclass, 's0.ss0', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 2
   AND relname = 's0.ss0';
 id | relid  | relname | relpages | reltuples | relallvisible | curpages 
----+--------+---------+----------+-----------+---------------+----------
  2 | s0.ss0 | s0.ss0  |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 's0.ss0', NULL);
 restore 
---------
(0 rows)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 2
   AND relname = 's0.ss0';
-- No.9-1-20
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (2, 's0.sc0'::regclass, 's0.sc0', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 2
   AND relname = 's0.sc0';
 id | relid  | relname | relpages | reltuples | relallvisible | curpages 
----+--------+---------+----------+-----------+---------------+----------
  2 | s0.sc0 | s0.sc0  |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 's0.sc0', NULL);
 restore 
---------
(0 rows)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 2
   AND relname = 's0.sc0';
-- No.9-1-21
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (3, 's0.sft0'::regclass, 's0.sft0', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 3
   AND relname = 's0.sft0';
 id |  relid  | relname | relpages | reltuples | relallvisible | curpages 
----+---------+---------+----------+-----------+---------------+----------
  3 | s0.sft0 | s0.sft0 |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 's0.sft0', NULL);
 restore 
---------
 s0.sft0
(1 row)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 3
   AND relname = 's0.sft0';
-- No.9-1-22
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (3, 's0.smv0'::regclass, 's0.smv0', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 3
   AND relname = 's0.smv0';
 id |  relid  | relname | relpages | reltuples | relallvisible | curpages 
----+---------+---------+----------+-----------+---------------+----------
  3 | s0.smv0 | s0.smv0 |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 's0.smv0', NULL);
 restore 
---------
 s0.smv0
(1 row)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 3
   AND relname = 's0.smv0';
-- No.9-1-23
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_backup(
               id, relid, relname, relpages, reltuples,
               relallvisible,
               curpages)
     VALUES (2, 'pg_catalog.pg_class'::regclass, 'pg_catalog.pg_class', 1, 1,
             1,
             1);
SELECT * FROM relations_backup_v
 WHERE id = 2
   AND relname = 'pg_catalog.pg_class';
 id |  relid   |       relname       | relpages | reltuples | relallvisible | curpages 
----+----------+---------------------+----------+-----------+---------------+----------
  2 | pg_class | pg_catalog.pg_class |        1 |         1 |             1 |        1
(1 row)

SELECT dbms_stats.restore(2, 'pg_catalog.pg_class', NULL);
 restore 
---------
(0 rows)

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

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

DELETE FROM dbms_stats.relation_stats_backup
 WHERE id = 2
   AND relname = 'pg_catalog.pg_class';
-- No.9-1-24
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_locked(relid, relname)
    VALUES ('s0.st0'::regclass, 's0.st0');
INSERT INTO dbms_stats.column_stats_locked(starelid, staattnum, stainherit)
     SELECT starelid::regclass, staattnum, stainherit
       FROM dbms_stats.column_stats_effective
      WHERE starelid = 's0.st0'::regclass;
SELECT id, unit, comment FROM dbms_stats.backup_history
 WHERE id = 2;
 id | unit | comment 
----+------+---------
  2 | d    | (null)
(1 row)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
(4 rows)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT dbms_stats.restore(2, 's0.st0', NULL);
 restore 
---------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

-- No.9-1-25
DELETE FROM dbms_stats.relation_stats_locked;
SELECT id, unit, comment FROM dbms_stats.backup_history
 WHERE id = 2;
 id | unit | comment 
----+------+---------
  2 | d    | (null)
(1 row)

SELECT dbms_stats.restore(2, 's0.st0', NULL);
 restore 
---------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

/*
 * Stab function dbms_stats.restore
 */
CREATE OR REPLACE FUNCTION dbms_stats.restore(
    backup_id int8,
    relid regclass DEFAULT NULL,
    attname text DEFAULT NULL)
RETURNS SETOF regclass AS
$$
BEGIN
    RAISE NOTICE 'arguments are "%, %, %"', $1, $2, $3;
    RETURN QUERY
        SELECT c.oid::regclass
          FROM pg_class c, dbms_stats.relation_stats_backup b
         WHERE (c.oid = $2 OR $2 IS NULL)
           AND c.oid = b.relid
           AND c.relkind IN ('r', 'i')
           AND (b.id <= $1 OR $1 IS NOT NULL)
         GROUP BY c.oid
         ORDER BY c.oid::regclass::text;
END;
$$
LANGUAGE plpgsql;
/*
 * No.10-1 dbms_stats.restore_database_stats
 */
-- No.10-1-1
SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
NOTICE:  arguments are "2, pt0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, pt0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_exp, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
 restore_database_stats 
------------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
(13 rows)

-- No.10-1-2
SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57.000002');
NOTICE:  arguments are "2, pt0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, pt0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "3, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_exp, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
 restore_database_stats 
------------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
(13 rows)

-- No.10-1-3
SELECT dbms_stats.restore_database_stats('2012-01-01 00:00:00');
 restore_database_stats 
------------------------
(0 rows)

--#No.10-1-4 is skipped after lock tests
--#No.10-1-5 is skipped after lock tests
-- No.10-1-6
SELECT dbms_stats.restore_database_stats('2012-02-29 23:59:57');
NOTICE:  arguments are "2, pt0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, pt0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_idx, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, st1_exp, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  SQL function "restore_database_stats" statement 1
 restore_database_stats 
------------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
(13 rows)

/*
 * No.10-2 dbms_stats.restore_schema_stats
 */
-- No.10-2-1
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
 restore_schema_stats 
----------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

-- No.10-2-2
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57.000002');
NOTICE:  arguments are "3, s0.st0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
 restore_schema_stats 
----------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

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

--#No.10-2-4 is skipped after lock tests
--#No.10-2-5 is skipped after lock tests
-- No.10-2-6
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
 restore_schema_stats 
----------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

-- No.10-2-7
SELECT dbms_stats.restore_schema_stats('s0', '2012-02-29 23:59:57');
NOTICE:  arguments are "2, s0.st0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st0_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st1_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.st2_idx, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.sft0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
NOTICE:  arguments are "2, s0.smv0, <NULL>"
CONTEXT:  PL/pgSQL function dbms_stats.restore_schema_stats(text,timestamp with time zone) line 10 at RETURN QUERY
 restore_schema_stats 
----------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

--#No.10-2-8 is skipped after lock tests
-- No.10-2-9
SELECT dbms_stats.restore_schema_stats('s00', '2012-02-29 23:59:57');
ERROR:  schema "s00" not found
-- No.10-2-10
SELECT dbms_stats.restore_schema_stats('pg_catalog', '2012-02-29 23:59:57');
ERROR:  restoring statistics is inhibited for system schemas: "pg_catalog"
/*
 * No.10-7 dbms_stats.restore_stats
 */
-- No.10-7-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore_stats(NULL);
 restore_stats 
---------------
(0 rows)

-- No.10-7-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.restore_stats(0);
ERROR:  backup id 0 not found
-- No.10-7-3
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.restore_stats(2);
 restore_stats 
---------------
 pt0
 pt0_idx
 s0.sft0
 s0.smv0
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st0
 st0_idx
 st1
 st1_exp
 st1_idx
(15 rows)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.backup_history        | AccessShareLock
 dbms_stats.column_stats_backup   | AccessShareLock
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_backup | AccessShareLock
 dbms_stats.relation_stats_backup | RowShareLock
 dbms_stats.relation_stats_locked | ExclusiveLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(8 rows)

COMMIT;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
(15 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
(17 rows)

-- No.10-7-4
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_locked(relid, relname)
     SELECT relid::regclass, relname
       FROM dbms_stats.relation_stats_effective;
INSERT INTO dbms_stats.column_stats_locked(starelid, staattnum, stainherit)
     SELECT starelid::regclass, staattnum, stainherit
       FROM dbms_stats.column_stats_effective;
SELECT id, unit, comment FROM dbms_stats.backup_history
 WHERE id = 8;
 id | unit | comment 
----+------+---------
  8 | s    | (null)
(1 row)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
 st0      |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 st0      |         2 | name    | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 st1      |         1 | val     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 st1      |         2 | str     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st1   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st1   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st2   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.st2   |         2 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 st1_exp  |         1 | lower   | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.sft0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         3 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s1.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
 s1.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)     | (null)     | (null)     | (null)     | (null)
(19 rows)

SELECT * FROM relations_locked_v;
   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
------------+----------------+----------+-----------+---------------+----------
 pt0        | public.pt0     |   (null) |    (null) |        (null) |   (null)
 pt0_idx    | public.pt0_idx |   (null) |    (null) |        (null) |   (null)
 st0        | public.st0     |   (null) |    (null) |        (null) |   (null)
 st0_idx    | public.st0_idx |   (null) |    (null) |        (null) |   (null)
 st1        | public.st1     |   (null) |    (null) |        (null) |   (null)
 s0.st0     | s0.st0         |   (null) |    (null) |        (null) |   (null)
 s0.st0_idx | s0.st0_idx     |   (null) |    (null) |        (null) |   (null)
 s0.st1     | s0.st1         |   (null) |    (null) |        (null) |   (null)
 s0.st1_idx | s0.st1_idx     |   (null) |    (null) |        (null) |   (null)
 s0.st2     | s0.st2         |   (null) |    (null) |        (null) |   (null)
 s0.st2_idx | s0.st2_idx     |   (null) |    (null) |        (null) |   (null)
 st1_idx    | public.st1_idx |   (null) |    (null) |        (null) |   (null)
 st1_exp    | public.st1_exp |   (null) |    (null) |        (null) |   (null)
 s0.sft0    | s0.sft0        |   (null) |    (null) |        (null) |   (null)
 s0.smv0    | s0.smv0        |   (null) |    (null) |        (null) |   (null)
 s1.st0     | s1.st0         |   (null) |    (null) |        (null) |   (null)
(16 rows)

SELECT dbms_stats.restore_stats(8);
 restore_stats 
---------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

SELECT * FROM relations_locked_v;
   relid    |    relname     | relpages | reltuples | relallvisible | curpages 
------------+----------------+----------+-----------+---------------+----------
 pt0        | public.pt0     |   (null) |    (null) |        (null) |   (null)
 pt0_idx    | public.pt0_idx |   (null) |    (null) |        (null) |   (null)
 st0        | public.st0     |   (null) |    (null) |        (null) |   (null)
 st0_idx    | public.st0_idx |   (null) |    (null) |        (null) |   (null)
 st1        | public.st1     |   (null) |    (null) |        (null) |   (null)
 s0.st0     | s0.st0         |        1 |         2 |             1 |        1
 s0.st0_idx | s0.st0_idx     |        2 |         2 |             0 |        2
 s0.st1     | s0.st1         |        1 |         3 |             1 |        1
 s0.st1_idx | s0.st1_idx     |        2 |         3 |             0 |        2
 s0.st2     | s0.st2         |        1 |         3 |             1 |        1
 s0.st2_idx | s0.st2_idx     |        2 |         3 |             0 |        2
 st1_idx    | public.st1_idx |   (null) |    (null) |        (null) |   (null)
 st1_exp    | public.st1_exp |   (null) |    (null) |        (null) |   (null)
 s0.sft0    | s0.sft0        |   (null) |    (null) |        (null) |   (null)
 s0.smv0    | s0.smv0        |   (null) |    (null) |        (null) |   (null)
 s1.st0     | s1.st0         |   (null) |    (null) |        (null) |   (null)
(16 rows)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 st0      |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 st0      |         2 | name    | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 st1      |         1 | val     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 st1      |         2 | str     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
 s0.st1   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}          | (null)     | (null)     | (null)     | (null)
 s0.st1   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {40,50,60}       | (null)     | (null)     | (null)     | (null)
 s0.st2   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}          | (null)     | (null)     | (null)     | (null)
 s0.st2   |         2 | txt     | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test} | (null)     | (null)     | (null)     | (null)
 st1_exp  |         1 | lower   | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s0.sft0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         3 | txt     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s1.st0   |         1 | id      | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
 s1.st0   |         2 | num     | f          |      (null) |   (null) |      (null) |   (null) |   (null) |   (null) |   (null) |   (null) | (null) | (null) | (null) | (null) | (null) | (null)      | (null)      | (null)      | (null)      | (null)      | (null)           | (null)     | (null)     | (null)     | (null)
(19 rows)

-- No.10-7-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT id, unit, comment FROM dbms_stats.backup_history
 WHERE id = 8;
 id | unit | comment 
----+------+---------
  8 | s    | (null)
(1 row)

SELECT dbms_stats.restore_stats(8);
 restore_stats 
---------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
(6 rows)

SELECT * FROM relations_locked_v;
   relid    |  relname   | relpages | reltuples | relallvisible | curpages 
------------+------------+----------+-----------+---------------+----------
 s0.st0     | s0.st0     |        1 |         2 |             1 |        1
 s0.st0_idx | s0.st0_idx |        2 |         2 |             0 |        2
 s0.st1     | s0.st1     |        1 |         3 |             1 |        1
 s0.st1_idx | s0.st1_idx |        2 |         3 |             0 |        2
 s0.st2     | s0.st2     |        1 |         3 |             1 |        1
 s0.st2_idx | s0.st2_idx |        2 |         3 |             0 |        2
(6 rows)

SELECT * FROM columns_locked_v;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
 s0.st1   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {4,5,6}          | (null)     | (null)     | (null)     | (null)
 s0.st1   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {40,50,60}       | (null)     | (null)     | (null)     | (null)
 s0.st2   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3}          | (null)     | (null)     | (null)     | (null)
 s0.st2   |         2 | txt     | f          |           0 |        5 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {0.5}       | (null)      | (null)      | (null)      | {1,comment,test} | (null)     | (null)     | (null)     | (null)
(8 rows)

/*
 * No.11-1 dbms_stats.lock(relid, attname)
 */
-- No.11-1-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock(NULL, NULL);
ERROR:  relation required
-- No.11-1-2
ALTER FUNCTION dbms_stats.lock(relid regclass)
    RENAME TO truth_lock;
CREATE FUNCTION dbms_stats.lock(relid regclass)
RETURNS regclass AS
$$
BEGIN
	RAISE NOTICE 'arguments are %', $1;
	RETURN $1;
END
$$
LANGUAGE plpgsql;
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0', NULL);
NOTICE:  arguments are s0.st0
CONTEXT:  PL/pgSQL function dbms_stats.lock(regclass,text) line 11 at RETURN
  lock  
--------
 s0.st0
(1 row)

DROP FUNCTION dbms_stats.lock(relid regclass);
ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
    RENAME TO lock;
-- No.11-1-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock(NULL, 'id');
ERROR:  relation required
-- No.11-1-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0', 'id');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
(2 rows)

-- No.11-1-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock(0, 'id');
ERROR:  relation "-" not found
-- No.11-1-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0', 'id');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
(2 rows)

-- No.11-1-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('pg_toast.pg_toast_2618', 'id');
ERROR:  "pg_toast.pg_toast_2618" must be a table or an index
-- No.11-1-8
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0_idx', 'id');
ERROR:  "s0.st0_idx" must be an expression index
-- No.11-1-9
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('st1_exp', 'lower');
  lock   
---------
 st1_exp
(1 row)

SELECT * FROM relations_locked_v;
  relid  |    relname     | relpages | reltuples | relallvisible | curpages 
---------+----------------+----------+-----------+---------------+----------
 st1_exp | public.st1_exp |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 |      stanumbers1       | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+------------------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
 st1_exp  |         1 | lower   | f          |           0 |        5 |           3 |        1 |        3 |        0 |        0 |        0 |     98 |    664 |      0 |      0 |      0 | {0.3334,0.3333,0.3333} | {0.3332}    | (null)      | (null)      | (null)      | {1,0,2}    | (null)     | (null)     | (null)     | (null)
(1 row)

DELETE FROM dbms_stats.relation_stats_locked;
-- No.11-1-10
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.ss0', 'id');
ERROR:  "s0.ss0" must be a table or an index
-- No.11-1-11
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.sc0', 'id');
ERROR:  "s0.sc0" must be a table or an index
-- No.11-1-12
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.sft0', 'id');
  lock   
---------
 s0.sft0
(1 row)

SELECT * FROM relations_locked_v;
  relid  | relname | relpages | reltuples | relallvisible | curpages 
---------+---------+----------+-----------+---------------+----------
 s0.sft0 | s0.sft0 |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
 s0.sft0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
(1 row)

-- No.11-1-13
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.smv0', 'id');
  lock   
---------
 s0.smv0
(1 row)

SELECT * FROM relations_locked_v;
  relid  | relname | relpages | reltuples | relallvisible | curpages 
---------+---------+----------+-----------+---------------+----------
 s0.smv0 | s0.smv0 |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
 s0.smv0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}      | (null)     | (null)     | (null)     | (null)
(1 row)

-- No.11-1-14
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('pg_catalog.pg_class', 'id');
ERROR:  locking statistics is inhibited for system catalogs: "pg_class"
-- No.11-1-15
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0', 'dummy');
ERROR:  column "dummy" not found in relation "s0.st0"
-- No.11-1-16
DELETE FROM dbms_stats.relation_stats_locked;
DELETE FROM pg_statistic
 WHERE starelid = 's0.st0'::regclass;
SELECT dbms_stats.lock('s0.st0', 'id');
ERROR:  no statistics available for column "id" of relation "s0.st0"
VACUUM ANALYZE;
-- No.11-1-17
DELETE FROM dbms_stats.relation_stats_locked;
INSERT INTO dbms_stats.relation_stats_locked(
    relid, relname, relpages, reltuples,
    relallvisible,
    curpages)
    VALUES('s0.st0'::regclass, 's0.st0', 1, 1640,
           1,
           1);
SELECT dbms_stats.lock_column_stats('s0.st0','id');
 lock_column_stats 
-------------------
 s0.st0
(1 row)

UPDATE dbms_stats.column_stats_locked
   SET (stanullfrac, stawidth, stadistinct,
        stakind1, stakind2, stakind3, stakind4,
        stakind5,
        staop1, staop2, staop3, staop4,
        staop5,
        stanumbers1, stanumbers2, stanumbers3, stanumbers4,
        stanumbers5,
        stavalues1, stavalues2, stavalues3, stavalues4
       ,stavalues5
       ) = (
        NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL,
        NULL, NULL, NULL, NULL)
 WHERE starelid = 's0.st0'::regclass;
SELECT dbms_stats.lock('s0.st0', 'id');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |      1640 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1  | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}       | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6} | (null)     | (null)     | (null)     | (null)
(2 rows)

-- No.11-1-18
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0', 'id');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v
 WHERE relid = 's0.st0'::regclass;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |   (null) |    (null) |        (null) |   (null)
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 s0.st0   | id      | f
 s0.st0   | id      | t
(2 rows)

/*
 * No.11-2 dbms_stats.lock(relid)
 */
-- No.11-2-1
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.lock('s0.st0');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | AccessShareLock
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | AccessShareLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(5 rows)

COMMIT;
-- No.11-2-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock(NULL);
ERROR:  relation required
-- No.11-2-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('0');
ERROR:  relation "-" not found
-- No.11-2-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

-- No.11-2-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('pg_toast.pg_toast_2618');
ERROR:  locking statistics is not allowed for relations with relkind "t": "pg_toast.pg_toast_2618"
HINT:  Only tables(r, m, f) and indexes(i) are lockable.
-- No.11-2-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0_idx');
    lock    
------------
 s0.st0_idx
(1 row)

SELECT * FROM relations_locked_v;
   relid    |  relname   | relpages | reltuples | relallvisible | curpages 
------------+------------+----------+-----------+---------------+----------
 s0.st0_idx | s0.st0_idx |        2 |         2 |             0 |        2
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
(0 rows)

-- No.11-2-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.ss0');
ERROR:  locking statistics is not allowed for relations with relkind "S": "s0.ss0"
HINT:  Only tables(r, m, f) and indexes(i) are lockable.
-- No.11-2-8
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.sc0');
ERROR:  locking statistics is not allowed for relations with relkind "c": "s0.sc0"
HINT:  Only tables(r, m, f) and indexes(i) are lockable.
-- No.11-2-9
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.sft0');
  lock   
---------
 s0.sft0
(1 row)

SELECT * FROM relations_locked_v;
  relid  | relname | relpages | reltuples | relallvisible | curpages 
---------+---------+----------+-----------+---------------+----------
 s0.sft0 | s0.sft0 |        1 |        10 |             0 |        0
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |       stavalues1       | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------------+------------+------------+------------+------------
 s0.sft0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,3,4,5,6,7,8,9,10} | (null)     | (null)     | (null)     | (null)
(1 row)

-- No.11-2-10
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.smv0');
  lock   
---------
 s0.smv0
(1 row)

SELECT * FROM relations_locked_v;
  relid  | relname | relpages | reltuples | relallvisible | curpages 
---------+---------+----------+-----------+---------------+----------
 s0.smv0 | s0.smv0 |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 | stavalues1 | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------+------------+------------+------------+------------
 s0.smv0  |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}      | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}    | (null)     | (null)     | (null)     | (null)
 s0.smv0  |         3 | txt     | f          |           0 |        3 |          -1 |        2 |        3 |        0 |        0 |        0 |    664 |    664 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,test}   | (null)     | (null)     | (null)     | (null)
(3 rows)

-- No.11-2-11
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('pg_catalog.pg_class');
ERROR:  locking statistics is not allowed for system catalogs: "pg_class"
-- No.11-2-12
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_table_stats('s0.st0');
 lock_table_stats 
------------------
 s0.st0
(1 row)

UPDATE dbms_stats.relation_stats_locked
   SET (relpages, reltuples,
        relallvisible,
        curpages)
     = (NULL, NULL, NULL
       ,NULL
       )
 WHERE relid = 's0.st0'::regclass;
SELECT dbms_stats.lock('s0.st0');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

-- No.11-2-13
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock('s0.st0');
  lock  
--------
 s0.st0
(1 row)

SELECT * FROM relations_locked_v;
 relid  | relname | relpages | reltuples | relallvisible | curpages 
--------+---------+----------+-----------+---------------+----------
 s0.st0 | s0.st0  |        1 |         2 |             1 |        1
(1 row)

SELECT * FROM columns_locked_v c;
 starelid | staattnum | attname | stainherit | stanullfrac | stawidth | stadistinct | stakind1 | stakind2 | stakind3 | stakind4 | stakind5 | staop1 | staop2 | staop3 | staop4 | staop5 | stanumbers1 | stanumbers2 | stanumbers3 | stanumbers4 | stanumbers5 |    stavalues1    | stavalues2 | stavalues3 | stavalues4 | stavalues5 
----------+-----------+---------+------------+-------------+----------+-------------+----------+----------+----------+----------+----------+--------+--------+--------+--------+--------+-------------+-------------+-------------+-------------+-------------+------------------+------------+------------+------------+------------
 s0.st0   |         1 | id      | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2}            | (null)     | (null)     | (null)     | (null)
 s0.st0   |         1 | id      | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {1,2,4,5,6}      | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | f          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20}          | (null)     | (null)     | (null)     | (null)
 s0.st0   |         2 | num     | t          |           0 |        4 |          -1 |        2 |        3 |        0 |        0 |        0 |     97 |     97 |      0 |      0 |      0 | (null)      | {1}         | (null)      | (null)      | (null)      | {10,20,40,50,60} | (null)     | (null)     | (null)     | (null)
(4 rows)

/*
 * Stab function dbms_stats.lock
 */
ALTER FUNCTION dbms_stats.lock(relid regclass)
    RENAME TO truth_lock;
CREATE FUNCTION dbms_stats.lock(relid regclass)
RETURNS regclass AS
$$
BEGIN
    RAISE NOTICE 'arguments are %', $1;
    RETURN $1;
END
$$
LANGUAGE plpgsql;
ALTER FUNCTION dbms_stats.lock(relid regclass, attname text)
    RENAME TO truth_lock;
CREATE FUNCTION dbms_stats.lock(
    relid regclass,
    attname text)
RETURNS regclass AS
$$
BEGIN
    RAISE NOTICE 'arguments are %, %', $1, $2;
    RETURN $1;
END
$$
LANGUAGE plpgsql;
/*
 * No.12-1 dbms_stats.lock_database_stats
 */
-- No.12-1-1
SELECT dbms_stats.lock_database_stats();
NOTICE:  arguments are pt0
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are pt0_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are st0
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are st0_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are st1
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st0
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st0_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st1
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st1_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st2
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.st2_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are st1_idx
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are st1_exp
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.sft0
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s0.smv0
CONTEXT:  SQL function "lock_database_stats" statement 1
NOTICE:  arguments are s1.st0
CONTEXT:  SQL function "lock_database_stats" statement 1
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

/*
 * No.12-2 dbms_stats.lock_schema_stats
 */
-- No.12-2-1
SELECT dbms_stats.lock_schema_stats('s0');
NOTICE:  arguments are s0.st0
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.st0_idx
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.st1
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.st1_idx
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.st2
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.st2_idx
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.sft0
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
NOTICE:  arguments are s0.smv0
CONTEXT:  PL/pgSQL function dbms_stats.lock_schema_stats(text) line 10 at RETURN QUERY
 lock_schema_stats 
-------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s0.sft0
 s0.smv0
(8 rows)

-- No.12-2-2
SELECT dbms_stats.lock_schema_stats('s00');
ERROR:  schema "s00" not found
-- No.12-2-3
SELECT dbms_stats.lock_schema_stats('pg_catalog');
ERROR:  locking statistics is not allowed for system schemas: "pg_catalog"
/*
 * No.12-3 dbms_stats.lock_table_stats(regclass)
 */
-- No.12-3-1
SELECT dbms_stats.lock_table_stats('s0.st0');
NOTICE:  arguments are s0.st0
CONTEXT:  SQL function "lock_table_stats" statement 1
 lock_table_stats 
------------------
 s0.st0
(1 row)

-- No.12-3-2
SELECT dbms_stats.lock_table_stats('st0');
NOTICE:  arguments are st0
CONTEXT:  SQL function "lock_table_stats" statement 1
 lock_table_stats 
------------------
 st0
(1 row)

-- No.12-3-3
SELECT dbms_stats.lock_table_stats('s00.s0');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.lock_table_stats('s00.s0');
                                           ^
/*
 * No.12-4 dbms_stats.lock_table_stats(schemaname, tablename)
 */
-- No.12-4-1
SELECT dbms_stats.lock_table_stats('s0', 'st0');
NOTICE:  arguments are s0.st0
CONTEXT:  SQL function "lock_table_stats" statement 1
 lock_table_stats 
------------------
 s0.st0
(1 row)

/*
 * No.12-5 dbms_stats.lock_column_stats(regclass, attname)
 */
-- No.12-5-1
SELECT dbms_stats.lock_column_stats('s0.st0', 'id');
NOTICE:  arguments are s0.st0, id
CONTEXT:  SQL function "lock_column_stats" statement 1
 lock_column_stats 
-------------------
 s0.st0
(1 row)

-- No.12-5-2
SELECT dbms_stats.lock_column_stats('st0', 'id');
NOTICE:  arguments are st0, id
CONTEXT:  SQL function "lock_column_stats" statement 1
 lock_column_stats 
-------------------
 st0
(1 row)

-- No.12-5-3
SELECT dbms_stats.lock_column_stats('s00.s0', 'id');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.lock_column_stats('s00.s0', 'id');
                                            ^
/*
 * No.12-6 dbms_stats.lock_column_stats(schemaname, tablename, int2)
 */
-- No.12-6-1
SELECT dbms_stats.lock_column_stats('s0', 'st0', 'id');
NOTICE:  arguments are s0.st0, id
CONTEXT:  SQL function "lock_column_stats" statement 1
 lock_column_stats 
-------------------
 s0.st0
(1 row)

/*
 * Delete Stab function lock
 */
DROP FUNCTION dbms_stats.lock(relid regclass);
ALTER FUNCTION dbms_stats.truth_lock(relid regclass)
    RENAME TO lock;
DROP FUNCTION dbms_stats.lock(relid regclass, attname text);
ALTER FUNCTION dbms_stats.truth_lock(relid regclass, attname text)
    RENAME TO lock;
/*
 * No.13-1 dbms_stats.unlock
 */
-- No.13-1-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

SELECT dbms_stats.unlock();
   unlock   
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.13-1-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT dbms_stats.unlock();
   unlock   
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

-- No.13-1-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT dbms_stats.unlock();
   unlock   
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

-- No.13-1-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.unlock();
 unlock 
--------
(0 rows)

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

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

-- No.13-1-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock('s0.st0');
 unlock 
--------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

-- No.13-1-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock('st0');
 unlock 
--------
 st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

-- No.13-1-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

SELECT dbms_stats.unlock('s00.s0');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.unlock('s00.s0');
                                 ^
SELECT count(*) FROM dbms_stats.relation_stats_locked;
 count 
-------
    16
(1 row)

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

-- No.13-1-8
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock('s0.st0', 'id');
 unlock 
--------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

-- No.13-1-9
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock('s0.st0', 'dummy');
ERROR:  column "dummy" not found in relation "s0.st0"
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

-- No.13-1-10
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT dbms_stats.unlock('s0.st0', 'id');
 unlock 
--------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

-- No.13-1-11
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_locked
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
 st1      |         1
 st1      |         2
 s0.st0   |         1
 s0.st0   |         2
 s0.st1   |         1
 s0.st1   |         2
 s0.st2   |         1
 s0.st2   |         2
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
 s0.smv0  |         2
 s0.smv0  |         3
 s1.st0   |         1
 s1.st0   |         2
(17 rows)

SELECT dbms_stats.unlock(NULL, 'id');
ERROR:  relation required
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_locked
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
 st1      |         1
 st1      |         2
 s0.st0   |         1
 s0.st0   |         2
 s0.st1   |         1
 s0.st1   |         2
 s0.st2   |         1
 s0.st2   |         2
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
 s0.smv0  |         2
 s0.smv0  |         3
 s1.st0   |         1
 s1.st0   |         2
(17 rows)

-- No.13-1-12
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_locked
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
 st1      |         1
 st1      |         2
 s0.st0   |         1
 s0.st0   |         2
 s0.st1   |         1
 s0.st1   |         2
 s0.st2   |         1
 s0.st2   |         2
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
 s0.smv0  |         2
 s0.smv0  |         3
 s1.st0   |         1
 s1.st0   |         2
(17 rows)

SELECT dbms_stats.unlock('s0.st0', NULL);
 unlock 
--------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, staattnum FROM dbms_stats.column_stats_locked
 GROUP BY starelid, staattnum
 ORDER BY starelid, staattnum;
 starelid | staattnum 
----------+-----------
 st0      |         1
 st0      |         2
 st1      |         1
 st1      |         2
 s0.st1   |         1
 s0.st1   |         2
 s0.st2   |         1
 s0.st2   |         2
 st1_exp  |         1
 s0.sft0  |         1
 s0.smv0  |         1
 s0.smv0  |         2
 s0.smv0  |         3
 s1.st0   |         1
 s1.st0   |         2
(15 rows)

-- No.13-1-13
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock();
   unlock   
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | AccessShareLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(4 rows)

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

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

/*
 * No.14-1 dbms_stats.unlock_database_stats
 */
-- No.14-1-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

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

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

SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-1-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

-- No.14-1-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
(0 rows)

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

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

-- No.14-1-4
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

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

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

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_database_stats();
 unlock_database_stats 
-----------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | AccessShareLock
 dbms_stats.relation_stats_locked | ExclusiveLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
(4 rows)

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

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

/*
 * No.14-2 dbms_stats.unlock_schema_stats
 */
-- No.14-2-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_schema_stats('s0');
 unlock_schema_stats 
---------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s0.sft0
 s0.smv0
(8 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
  relid  
---------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 st1_idx
 st1_exp
 s1.st0
(8 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 st1_exp  |     1
 s1.st0   |     2
(4 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-2-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_schema_stats('s0');
 unlock_schema_stats 
---------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s0.sft0
 s0.smv0
(8 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
  relid  
---------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 st1_idx
 st1_exp
 s1.st0
(8 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-2-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_schema_stats('s0');
 unlock_schema_stats 
---------------------
(0 rows)

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

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-2-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_schema_stats('s0');
 unlock_schema_stats 
---------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s0.sft0
 s0.smv0
(8 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
  relid  
---------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 st1_idx
 st1_exp
 s1.st0
(8 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 st1_exp  |     1
 s1.st0   |     2
(4 rows)

-- No.14-2-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_schema_stats('s00');
ERROR:  schema "s00" not found
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-2-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_schema_stats('pg_catalog');
ERROR:  unlocking statistics is not allowed for system schemas: "pg_catalog"
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-2-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_schema_stats(NULL);
 unlock_schema_stats 
---------------------
(0 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-2-8
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_schema_stats('s0');
 unlock_schema_stats 
---------------------
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 s0.sft0
 s0.smv0
(8 rows)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(3 rows)

COMMIT;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
  relid  
---------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 st1_idx
 st1_exp
 s1.st0
(8 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 st1_exp  |     1
 s1.st0   |     2
(4 rows)

/*
 * No.14-3 dbms_stats.unlock_table_stats(regclass)
 */
-- No.14-3-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s0.st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-3-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0.st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-3-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0.st0');
 unlock_table_stats 
--------------------
(0 rows)

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

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-3-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s0.st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

-- No.14-3-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('st0');
 unlock_table_stats 
--------------------
 st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

-- No.14-3-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s00.s0');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.unlock_table_stats('s00.s0');
                                             ^
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-3-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats(NULL);
 unlock_table_stats 
--------------------
(0 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-3-8
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0.st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
(2 rows)

COMMIT;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

/*
 * No.14-4 dbms_stats.unlock_table_stats(schemaname, tablename)
 */
-- No.14-4-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s0','st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-4-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0', 'st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-4-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
 relid 
-------
(0 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0', 'st0');
 unlock_table_stats 
--------------------
(0 rows)

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

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
(0 rows)

-- No.14-4-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s0', 'st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

-- No.14-4-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s00', 's0');
ERROR:  schema "s00" does not exist
CONTEXT:  SQL function "unlock_table_stats" statement 1
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-4-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats(NULL, 'st0');
 unlock_table_stats 
--------------------
(0 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-4-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

SELECT dbms_stats.unlock_table_stats('s0', NULL);
 unlock_table_stats 
--------------------
(0 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

-- No.14-4-8
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st0   |     4
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(9 rows)

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_table_stats('s0', 'st0');
 unlock_table_stats 
--------------------
 s0.st0
(1 row)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowExclusiveLock
(2 rows)

COMMIT;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(15 rows)

SELECT starelid::regclass, count(*) FROM dbms_stats.column_stats_locked
 GROUP BY starelid
 ORDER BY starelid;
 starelid | count 
----------+-------
 st0      |     2
 st1      |     2
 s0.st1   |     2
 s0.st2   |     2
 st1_exp  |     1
 s0.sft0  |     1
 s0.smv0  |     3
 s1.st0   |     2
(8 rows)

/*
 * No.14-5 dbms_stats.unlock_column_stats(regclass, attname)
 */
-- No.14-5-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-5-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

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

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('st0', 'id');
 unlock_column_stats 
---------------------
 st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(18 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', 'dummy');
ERROR:  column "dummy" not found in relation "s0.st0"
SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s00.s0', 'id');
ERROR:  schema "s00" does not exist
LINE 1: SELECT dbms_stats.unlock_column_stats('s00.s0', 'id');
                                              ^
SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats(NULL, 'id');
 unlock_column_stats 
---------------------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-8
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', NULL);
 unlock_column_stats 
---------------------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-5-9
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_column_stats('s0.st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(2 rows)

COMMIT;
SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

/*
 * No.14-6 dbms_stats.unlock_column_stats(schemaname, tablename, attname)
 */
-- No.14-6-1
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT * FROM dbms_stats.backup_history
 ORDER BY id;
 id |                time                 | unit | comment 
----+-------------------------------------+------+---------
  1 | Wed Feb 29 23:59:56.999999 2012 PST | d    | (null)
  2 | Wed Feb 29 23:59:57 2012 PST        | d    | (null)
  3 | Wed Feb 29 23:59:57.000001 2012 PST | t    | (null)
  4 | Wed Feb 29 23:59:58 2012 PST        | d    | (null)
  5 | Thu Mar 01 00:00:00 2012 PST        | c    | (null)
  6 | Thu Mar 01 00:00:02 2012 PST        | t    | (null)
  7 | Thu Mar 01 00:00:04 2012 PST        | t    | (null)
  8 | Thu Mar 01 00:00:06 2012 PST        | s    | (null)
(8 rows)

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

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

-- No.14-6-2
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

DELETE FROM dbms_stats.column_stats_locked;
SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
(0 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-3
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-4
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'dummy');
ERROR:  column "dummy" not found in relation "s0.st0"
SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-5
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats(NULL, 'st0', 'id');
 unlock_column_stats 
---------------------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-6
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0', NULL, 'id');
 unlock_column_stats 
---------------------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-7
DELETE FROM dbms_stats.relation_stats_locked;
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', NULL);
 unlock_column_stats 
---------------------
(0 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.14-6-8
DELETE FROM dbms_stats.relation_stats_locked;
VACUUM dbms_stats.relation_stats_locked;  -- in order to avoid auto vacuum
SELECT dbms_stats.lock_database_stats();
 lock_database_stats 
---------------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | id      | f
 s0.st0   | id      | t
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(19 rows)

BEGIN;
SELECT * FROM internal_locks;
 relation | mode 
----------+------
(0 rows)

SELECT dbms_stats.unlock_column_stats('s0', 'st0', 'id');
 unlock_column_stats 
---------------------
 s0.st0
(1 row)

SELECT * FROM internal_locks;
             relation             |       mode       
----------------------------------+------------------
 dbms_stats.column_stats_locked   | RowExclusiveLock
 dbms_stats.relation_stats_locked | RowShareLock
(2 rows)

COMMIT;
SELECT starelid, attname, stainherit FROM columns_locked_v c;
 starelid | attname | stainherit 
----------+---------+------------
 st0      | id      | f
 st0      | name    | f
 st1      | val     | f
 st1      | str     | f
 s0.st0   | num     | f
 s0.st0   | num     | t
 s0.st1   | id      | f
 s0.st1   | num     | f
 s0.st2   | id      | f
 s0.st2   | txt     | f
 st1_exp  | lower   | f
 s0.sft0  | id      | f
 s0.smv0  | id      | f
 s0.smv0  | num     | f
 s0.smv0  | txt     | f
 s1.st0   | id      | f
 s1.st0   | num     | f
(17 rows)

SELECT relid::regclass FROM dbms_stats.relation_stats_locked
 GROUP BY relid
 ORDER BY relid;
   relid    
------------
 pt0
 pt0_idx
 st0
 st0_idx
 st1
 s0.st0
 s0.st0_idx
 s0.st1
 s0.st1_idx
 s0.st2
 s0.st2_idx
 st1_idx
 st1_exp
 s0.sft0
 s0.smv0
 s1.st0
(16 rows)

-- No.15 error description. -- abnormal case.
RESET SESSION AUTHORIZATION;
CREATE TABLE s0.st4 (a int, b text);
CREATE VIEW s0.vst4 AS select * FROM s0.st4;
GRANT SELECT ON s0.vst4 TO regular_user;
ALTER TABLE dbms_stats.relation_stats_locked OWNER TO regular_user;
/* reconnection needed to flush cache */
\c - regular_user
EXPLAIN (COSTS OFF) SELECT * FROM s0.vst4 WHERE a = 1;
ERROR:  permission denied for schema dbms_stats
LINE 1: SELECT *   FROM dbms_stats.column_stats_locked WHERE stareli...
                        ^
DETAIL:  dbms_stats could not access the object as the role "regular_user".
HINT:  Check your settings of pg_dbms_stats.
QUERY:  SELECT *   FROM dbms_stats.column_stats_locked WHERE starelid = $1    AND staattnum = $2    AND stainherit = $3
\c - super_user
ALTER TABLE dbms_stats.relation_stats_locked OWNER TO super_user;
DROP TABLE s0.st4 CASCADE;
NOTICE:  drop cascades to view s0.vst4
