--- /dev/null
+\r
+\r
+\r
+CREATE VIEW pg_user AS \r
+ SELECT \r
+ usename, \r
+ usesysid, \r
+ usecreatedb, \r
+ usesuper, \r
+ usecatupd, \r
+ '********'::text as passwd, \r
+ valuntil, \r
+ useconfig \r
+ FROM pg_shadow;\r
+\r
+CREATE VIEW pg_rules AS \r
+ SELECT \r
+ N.nspname AS schemaname, \r
+ C.relname AS tablename, \r
+ R.rulename AS rulename, \r
+ pg_get_ruledef(R.oid) AS definition \r
+ FROM (pg_rewrite R JOIN pg_class C ON (C.oid = R.ev_class)) \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE R.rulename != '_RETURN';\r
+\r
+CREATE VIEW pg_views AS \r
+ SELECT \r
+ N.nspname AS schemaname, \r
+ C.relname AS viewname, \r
+ pg_get_userbyid(C.relowner) AS viewowner, \r
+ pg_get_viewdef(C.oid) AS definition \r
+ FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'v';\r
+\r
+CREATE VIEW pg_tables AS \r
+ SELECT \r
+ N.nspname AS schemaname, \r
+ C.relname AS tablename, \r
+ pg_get_userbyid(C.relowner) AS tableowner, \r
+ C.relhasindex AS hasindexes, \r
+ C.relhasrules AS hasrules, \r
+ (C.reltriggers > 0) AS hastriggers \r
+ FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r';\r
+\r
+CREATE VIEW pg_indexes AS \r
+ SELECT \r
+ N.nspname AS schemaname, \r
+ C.relname AS tablename, \r
+ I.relname AS indexname, \r
+ pg_get_indexdef(I.oid) AS indexdef \r
+ FROM pg_index X JOIN pg_class C ON (C.oid = X.indrelid) \r
+ JOIN pg_class I ON (I.oid = X.indexrelid) \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r' AND I.relkind = 'i';\r
+\r
+CREATE VIEW pg_stats AS \r
+ SELECT \r
+ nspname AS schemaname, \r
+ relname AS tablename, \r
+ attname AS attname, \r
+ stanullfrac AS null_frac, \r
+ stawidth AS avg_width, \r
+ stadistinct AS n_distinct, \r
+ CASE 1 \r
+ WHEN stakind1 THEN stavalues1 \r
+ WHEN stakind2 THEN stavalues2 \r
+ WHEN stakind3 THEN stavalues3 \r
+ WHEN stakind4 THEN stavalues4 \r
+ END AS most_common_vals, \r
+ CASE 1 \r
+ WHEN stakind1 THEN stanumbers1 \r
+ WHEN stakind2 THEN stanumbers2 \r
+ WHEN stakind3 THEN stanumbers3 \r
+ WHEN stakind4 THEN stanumbers4 \r
+ END AS most_common_freqs, \r
+ CASE 2 \r
+ WHEN stakind1 THEN stavalues1 \r
+ WHEN stakind2 THEN stavalues2 \r
+ WHEN stakind3 THEN stavalues3 \r
+ WHEN stakind4 THEN stavalues4 \r
+ END AS histogram_bounds, \r
+ CASE 3 \r
+ WHEN stakind1 THEN stanumbers1[1] \r
+ WHEN stakind2 THEN stanumbers2[1] \r
+ WHEN stakind3 THEN stanumbers3[1] \r
+ WHEN stakind4 THEN stanumbers4[1] \r
+ END AS correlation \r
+ FROM pg_statistic s JOIN pg_class c ON (c.oid = s.starelid) \r
+ JOIN pg_attribute a ON (c.oid = attrelid AND attnum = s.staattnum) \r
+ LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace) \r
+ WHERE has_table_privilege(c.oid, 'select');\r
+\r
+REVOKE ALL on pg_statistic FROM public;\r
+\r
+CREATE VIEW pg_stat_all_tables AS \r
+ SELECT \r
+ C.oid AS relid, \r
+ N.nspname AS schemaname, \r
+ C.relname AS relname, \r
+ pg_stat_get_numscans(C.oid) AS seq_scan, \r
+ pg_stat_get_tuples_returned(C.oid) AS seq_tup_read, \r
+ sum(pg_stat_get_numscans(I.indexrelid)) AS idx_scan, \r
+ sum(pg_stat_get_tuples_fetched(I.indexrelid)) AS idx_tup_fetch, \r
+ pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins, \r
+ pg_stat_get_tuples_updated(C.oid) AS n_tup_upd, \r
+ pg_stat_get_tuples_deleted(C.oid) AS n_tup_del \r
+ FROM pg_class C LEFT JOIN \r
+ pg_index I ON C.oid = I.indrelid \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r' \r
+ GROUP BY C.oid, N.nspname, C.relname;\r
+\r
+CREATE VIEW pg_stat_sys_tables AS \r
+ SELECT * FROM pg_stat_all_tables \r
+ WHERE schemaname IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_stat_user_tables AS \r
+ SELECT * FROM pg_stat_all_tables \r
+ WHERE schemaname NOT IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_all_tables AS \r
+ SELECT \r
+ C.oid AS relid, \r
+ N.nspname AS schemaname, \r
+ C.relname AS relname, \r
+ pg_stat_get_blocks_fetched(C.oid) - \r
+ pg_stat_get_blocks_hit(C.oid) AS heap_blks_read, \r
+ pg_stat_get_blocks_hit(C.oid) AS heap_blks_hit, \r
+ sum(pg_stat_get_blocks_fetched(I.indexrelid) - \r
+ pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_read, \r
+ sum(pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_hit, \r
+ pg_stat_get_blocks_fetched(T.oid) - \r
+ pg_stat_get_blocks_hit(T.oid) AS toast_blks_read, \r
+ pg_stat_get_blocks_hit(T.oid) AS toast_blks_hit, \r
+ pg_stat_get_blocks_fetched(X.oid) - \r
+ pg_stat_get_blocks_hit(X.oid) AS tidx_blks_read, \r
+ pg_stat_get_blocks_hit(X.oid) AS tidx_blks_hit \r
+ FROM pg_class C LEFT JOIN \r
+ pg_index I ON C.oid = I.indrelid LEFT JOIN \r
+ pg_class T ON C.reltoastrelid = T.oid LEFT JOIN \r
+ pg_class X ON T.reltoastidxid = X.oid \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r' \r
+ GROUP BY C.oid, N.nspname, C.relname, T.oid, X.oid;\r
+\r
+CREATE VIEW pg_statio_sys_tables AS \r
+ SELECT * FROM pg_statio_all_tables \r
+ WHERE schemaname IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_user_tables AS \r
+ SELECT * FROM pg_statio_all_tables \r
+ WHERE schemaname NOT IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_stat_all_indexes AS \r
+ SELECT \r
+ C.oid AS relid, \r
+ I.oid AS indexrelid, \r
+ N.nspname AS schemaname, \r
+ C.relname AS relname, \r
+ I.relname AS indexrelname, \r
+ pg_stat_get_numscans(I.oid) AS idx_scan, \r
+ pg_stat_get_tuples_returned(I.oid) AS idx_tup_read, \r
+ pg_stat_get_tuples_fetched(I.oid) AS idx_tup_fetch \r
+ FROM pg_class C JOIN \r
+ pg_index X ON C.oid = X.indrelid JOIN \r
+ pg_class I ON I.oid = X.indexrelid \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r';\r
+\r
+CREATE VIEW pg_stat_sys_indexes AS \r
+ SELECT * FROM pg_stat_all_indexes \r
+ WHERE schemaname IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_stat_user_indexes AS \r
+ SELECT * FROM pg_stat_all_indexes \r
+ WHERE schemaname NOT IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_all_indexes AS \r
+ SELECT \r
+ C.oid AS relid, \r
+ I.oid AS indexrelid, \r
+ N.nspname AS schemaname, \r
+ C.relname AS relname, \r
+ I.relname AS indexrelname, \r
+ pg_stat_get_blocks_fetched(I.oid) - \r
+ pg_stat_get_blocks_hit(I.oid) AS idx_blks_read, \r
+ pg_stat_get_blocks_hit(I.oid) AS idx_blks_hit \r
+ FROM pg_class C JOIN \r
+ pg_index X ON C.oid = X.indrelid JOIN \r
+ pg_class I ON I.oid = X.indexrelid \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'r';\r
+\r
+CREATE VIEW pg_statio_sys_indexes AS \r
+ SELECT * FROM pg_statio_all_indexes \r
+ WHERE schemaname IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_user_indexes AS \r
+ SELECT * FROM pg_statio_all_indexes \r
+ WHERE schemaname NOT IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_all_sequences AS \r
+ SELECT \r
+ C.oid AS relid, \r
+ N.nspname AS schemaname, \r
+ C.relname AS relname, \r
+ pg_stat_get_blocks_fetched(C.oid) - \r
+ pg_stat_get_blocks_hit(C.oid) AS blks_read, \r
+ pg_stat_get_blocks_hit(C.oid) AS blks_hit \r
+ FROM pg_class C \r
+ LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) \r
+ WHERE C.relkind = 'S';\r
+\r
+CREATE VIEW pg_statio_sys_sequences AS \r
+ SELECT * FROM pg_statio_all_sequences \r
+ WHERE schemaname IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_statio_user_sequences AS \r
+ SELECT * FROM pg_statio_all_sequences \r
+ WHERE schemaname NOT IN ('pg_catalog', 'pg_toast');\r
+\r
+CREATE VIEW pg_stat_activity AS \r
+ SELECT \r
+ D.oid AS datid, \r
+ D.datname AS datname, \r
+ pg_stat_get_backend_pid(S.backendid) AS procpid, \r
+ pg_stat_get_backend_userid(S.backendid) AS usesysid, \r
+ U.usename AS usename, \r
+ pg_stat_get_backend_activity(S.backendid) AS current_query, \r
+ pg_stat_get_backend_activity_start(S.backendid) AS query_start \r
+ FROM pg_database D, \r
+ (SELECT pg_stat_get_backend_idset() AS backendid) AS S, \r
+ pg_shadow U \r
+ WHERE pg_stat_get_backend_dbid(S.backendid) = D.oid AND \r
+ pg_stat_get_backend_userid(S.backendid) = U.usesysid;\r
+\r
+CREATE VIEW pg_stat_database AS \r
+ SELECT \r
+ D.oid AS datid, \r
+ D.datname AS datname, \r
+ pg_stat_get_db_numbackends(D.oid) AS numbackends, \r
+ pg_stat_get_db_xact_commit(D.oid) AS xact_commit, \r
+ pg_stat_get_db_xact_rollback(D.oid) AS xact_rollback, \r
+ pg_stat_get_db_blocks_fetched(D.oid) - \r
+ pg_stat_get_db_blocks_hit(D.oid) AS blks_read, \r
+ pg_stat_get_db_blocks_hit(D.oid) AS blks_hit \r
+ FROM pg_database D;\r
+\r
+CREATE VIEW pg_locks AS \r
+ SELECT * \r
+ FROM pg_lock_status() AS L(relation oid, database oid, \r
+ transaction xid, pid int4, mode text, granted boolean);\r
+\r
+CREATE VIEW pg_settings AS \r
+ SELECT * \r
+ FROM pg_show_all_settings() AS A \r
+ (name text, setting text, context text, vartype text, \r
+ source text, min_val text, max_val text);\r
+\r
+CREATE RULE pg_settings_u AS \r
+ ON UPDATE TO pg_settings \r
+ WHERE new.name = old.name DO \r
+ SELECT set_config(old.name, new.setting, 'f');\r
+\r
+CREATE RULE pg_settings_n AS \r
+ ON UPDATE TO pg_settings \r
+ DO INSTEAD NOTHING;\r
+\r
+\r