2 * PostgreSQL System Views
4 * Copyright (c) 1996-2009, PostgreSQL Global Development Group
9 CREATE VIEW pg_roles AS
19 '********'::text as rolpassword,
25 CREATE VIEW pg_shadow AS
29 rolcreatedb AS usecreatedb,
31 rolcatupdate AS usecatupd,
32 rolpassword AS passwd,
33 rolvaliduntil::abstime AS valuntil,
34 rolconfig AS useconfig
38 REVOKE ALL on pg_shadow FROM public;
40 CREATE VIEW pg_group AS
44 ARRAY(SELECT member FROM pg_auth_members WHERE roleid = oid) AS grolist
46 WHERE NOT rolcanlogin;
48 CREATE VIEW pg_user AS
55 '********'::text as passwd,
60 CREATE VIEW pg_rules AS
62 N.nspname AS schemaname,
63 C.relname AS tablename,
64 R.rulename AS rulename,
65 pg_get_ruledef(R.oid) AS definition
66 FROM (pg_rewrite R JOIN pg_class C ON (C.oid = R.ev_class))
67 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
68 WHERE R.rulename != '_RETURN';
70 CREATE VIEW pg_views AS
72 N.nspname AS schemaname,
73 C.relname AS viewname,
74 pg_get_userbyid(C.relowner) AS viewowner,
75 pg_get_viewdef(C.oid) AS definition
76 FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
77 WHERE C.relkind = 'v';
79 CREATE VIEW pg_tables AS
81 N.nspname AS schemaname,
82 C.relname AS tablename,
83 pg_get_userbyid(C.relowner) AS tableowner,
84 T.spcname AS tablespace,
85 C.relhasindex AS hasindexes,
86 C.relhasrules AS hasrules,
87 C.relhastriggers AS hastriggers
88 FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
89 LEFT JOIN pg_tablespace T ON (T.oid = C.reltablespace)
90 WHERE C.relkind = 'r';
92 CREATE VIEW pg_indexes AS
94 N.nspname AS schemaname,
95 C.relname AS tablename,
96 I.relname AS indexname,
97 T.spcname AS tablespace,
98 pg_get_indexdef(I.oid) AS indexdef
99 FROM pg_index X JOIN pg_class C ON (C.oid = X.indrelid)
100 JOIN pg_class I ON (I.oid = X.indexrelid)
101 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
102 LEFT JOIN pg_tablespace T ON (T.oid = I.reltablespace)
103 WHERE C.relkind = 'r' AND I.relkind = 'i';
105 CREATE VIEW pg_stats AS
107 nspname AS schemaname,
108 relname AS tablename,
110 stanullfrac AS null_frac,
111 stawidth AS avg_width,
112 stadistinct AS n_distinct,
114 WHEN stakind1 IN (1, 4) THEN stavalues1
115 WHEN stakind2 IN (1, 4) THEN stavalues2
116 WHEN stakind3 IN (1, 4) THEN stavalues3
117 WHEN stakind4 IN (1, 4) THEN stavalues4
118 END AS most_common_vals,
120 WHEN stakind1 IN (1, 4) THEN stanumbers1
121 WHEN stakind2 IN (1, 4) THEN stanumbers2
122 WHEN stakind3 IN (1, 4) THEN stanumbers3
123 WHEN stakind4 IN (1, 4) THEN stanumbers4
124 END AS most_common_freqs,
126 WHEN stakind1 = 2 THEN stavalues1
127 WHEN stakind2 = 2 THEN stavalues2
128 WHEN stakind3 = 2 THEN stavalues3
129 WHEN stakind4 = 2 THEN stavalues4
130 END AS histogram_bounds,
132 WHEN stakind1 = 3 THEN stanumbers1[1]
133 WHEN stakind2 = 3 THEN stanumbers2[1]
134 WHEN stakind3 = 3 THEN stanumbers3[1]
135 WHEN stakind4 = 3 THEN stanumbers4[1]
137 FROM pg_statistic s JOIN pg_class c ON (c.oid = s.starelid)
138 JOIN pg_attribute a ON (c.oid = attrelid AND attnum = s.staattnum)
139 LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
140 WHERE NOT attisdropped AND has_column_privilege(c.oid, a.attnum, 'select');
142 REVOKE ALL on pg_statistic FROM public;
144 CREATE VIEW pg_locks AS
145 SELECT * FROM pg_lock_status() AS L;
147 CREATE VIEW pg_cursors AS
148 SELECT * FROM pg_cursor() AS C;
150 CREATE VIEW pg_prepared_xacts AS
151 SELECT P.transaction, P.gid, P.prepared,
152 U.rolname AS owner, D.datname AS database
153 FROM pg_prepared_xact() AS P
154 LEFT JOIN pg_authid U ON P.ownerid = U.oid
155 LEFT JOIN pg_database D ON P.dbid = D.oid;
157 CREATE VIEW pg_prepared_statements AS
158 SELECT * FROM pg_prepared_statement() AS P;
160 CREATE VIEW pg_settings AS
161 SELECT * FROM pg_show_all_settings() AS A;
163 CREATE RULE pg_settings_u AS
164 ON UPDATE TO pg_settings
165 WHERE new.name = old.name DO
166 SELECT set_config(old.name, new.setting, 'f');
168 CREATE RULE pg_settings_n AS
169 ON UPDATE TO pg_settings
172 GRANT SELECT, UPDATE ON pg_settings TO PUBLIC;
174 CREATE VIEW pg_timezone_abbrevs AS
175 SELECT * FROM pg_timezone_abbrevs();
177 CREATE VIEW pg_timezone_names AS
178 SELECT * FROM pg_timezone_names();
182 CREATE VIEW pg_stat_all_tables AS
185 N.nspname AS schemaname,
186 C.relname AS relname,
187 pg_stat_get_numscans(C.oid) AS seq_scan,
188 pg_stat_get_tuples_returned(C.oid) AS seq_tup_read,
189 sum(pg_stat_get_numscans(I.indexrelid))::bigint AS idx_scan,
190 sum(pg_stat_get_tuples_fetched(I.indexrelid))::bigint +
191 pg_stat_get_tuples_fetched(C.oid) AS idx_tup_fetch,
192 pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins,
193 pg_stat_get_tuples_updated(C.oid) AS n_tup_upd,
194 pg_stat_get_tuples_deleted(C.oid) AS n_tup_del,
195 pg_stat_get_tuples_hot_updated(C.oid) AS n_tup_hot_upd,
196 pg_stat_get_live_tuples(C.oid) AS n_live_tup,
197 pg_stat_get_dead_tuples(C.oid) AS n_dead_tup,
198 pg_stat_get_last_vacuum_time(C.oid) as last_vacuum,
199 pg_stat_get_last_autovacuum_time(C.oid) as last_autovacuum,
200 pg_stat_get_last_analyze_time(C.oid) as last_analyze,
201 pg_stat_get_last_autoanalyze_time(C.oid) as last_autoanalyze
202 FROM pg_class C LEFT JOIN
203 pg_index I ON C.oid = I.indrelid
204 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
205 WHERE C.relkind IN ('r', 't')
206 GROUP BY C.oid, N.nspname, C.relname;
208 CREATE VIEW pg_stat_sys_tables AS
209 SELECT * FROM pg_stat_all_tables
210 WHERE schemaname IN ('pg_catalog', 'information_schema') OR
211 schemaname ~ '^pg_toast';
213 CREATE VIEW pg_stat_user_tables AS
214 SELECT * FROM pg_stat_all_tables
215 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND
216 schemaname !~ '^pg_toast';
218 CREATE VIEW pg_statio_all_tables AS
221 N.nspname AS schemaname,
222 C.relname AS relname,
223 pg_stat_get_blocks_fetched(C.oid) -
224 pg_stat_get_blocks_hit(C.oid) AS heap_blks_read,
225 pg_stat_get_blocks_hit(C.oid) AS heap_blks_hit,
226 sum(pg_stat_get_blocks_fetched(I.indexrelid) -
227 pg_stat_get_blocks_hit(I.indexrelid))::bigint AS idx_blks_read,
228 sum(pg_stat_get_blocks_hit(I.indexrelid))::bigint AS idx_blks_hit,
229 pg_stat_get_blocks_fetched(T.oid) -
230 pg_stat_get_blocks_hit(T.oid) AS toast_blks_read,
231 pg_stat_get_blocks_hit(T.oid) AS toast_blks_hit,
232 pg_stat_get_blocks_fetched(X.oid) -
233 pg_stat_get_blocks_hit(X.oid) AS tidx_blks_read,
234 pg_stat_get_blocks_hit(X.oid) AS tidx_blks_hit
235 FROM pg_class C LEFT JOIN
236 pg_index I ON C.oid = I.indrelid LEFT JOIN
237 pg_class T ON C.reltoastrelid = T.oid LEFT JOIN
238 pg_class X ON T.reltoastidxid = X.oid
239 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
240 WHERE C.relkind IN ('r', 't')
241 GROUP BY C.oid, N.nspname, C.relname, T.oid, X.oid;
243 CREATE VIEW pg_statio_sys_tables AS
244 SELECT * FROM pg_statio_all_tables
245 WHERE schemaname IN ('pg_catalog', 'information_schema') OR
246 schemaname ~ '^pg_toast';
248 CREATE VIEW pg_statio_user_tables AS
249 SELECT * FROM pg_statio_all_tables
250 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND
251 schemaname !~ '^pg_toast';
253 CREATE VIEW pg_stat_all_indexes AS
257 N.nspname AS schemaname,
258 C.relname AS relname,
259 I.relname AS indexrelname,
260 pg_stat_get_numscans(I.oid) AS idx_scan,
261 pg_stat_get_tuples_returned(I.oid) AS idx_tup_read,
262 pg_stat_get_tuples_fetched(I.oid) AS idx_tup_fetch
264 pg_index X ON C.oid = X.indrelid JOIN
265 pg_class I ON I.oid = X.indexrelid
266 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
267 WHERE C.relkind IN ('r', 't');
269 CREATE VIEW pg_stat_sys_indexes AS
270 SELECT * FROM pg_stat_all_indexes
271 WHERE schemaname IN ('pg_catalog', 'information_schema') OR
272 schemaname ~ '^pg_toast';
274 CREATE VIEW pg_stat_user_indexes AS
275 SELECT * FROM pg_stat_all_indexes
276 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND
277 schemaname !~ '^pg_toast';
279 CREATE VIEW pg_statio_all_indexes AS
283 N.nspname AS schemaname,
284 C.relname AS relname,
285 I.relname AS indexrelname,
286 pg_stat_get_blocks_fetched(I.oid) -
287 pg_stat_get_blocks_hit(I.oid) AS idx_blks_read,
288 pg_stat_get_blocks_hit(I.oid) AS idx_blks_hit
290 pg_index X ON C.oid = X.indrelid JOIN
291 pg_class I ON I.oid = X.indexrelid
292 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
293 WHERE C.relkind IN ('r', 't');
295 CREATE VIEW pg_statio_sys_indexes AS
296 SELECT * FROM pg_statio_all_indexes
297 WHERE schemaname IN ('pg_catalog', 'information_schema') OR
298 schemaname ~ '^pg_toast';
300 CREATE VIEW pg_statio_user_indexes AS
301 SELECT * FROM pg_statio_all_indexes
302 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND
303 schemaname !~ '^pg_toast';
305 CREATE VIEW pg_statio_all_sequences AS
308 N.nspname AS schemaname,
309 C.relname AS relname,
310 pg_stat_get_blocks_fetched(C.oid) -
311 pg_stat_get_blocks_hit(C.oid) AS blks_read,
312 pg_stat_get_blocks_hit(C.oid) AS blks_hit
314 LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
315 WHERE C.relkind = 'S';
317 CREATE VIEW pg_statio_sys_sequences AS
318 SELECT * FROM pg_statio_all_sequences
319 WHERE schemaname IN ('pg_catalog', 'information_schema') OR
320 schemaname ~ '^pg_toast';
322 CREATE VIEW pg_statio_user_sequences AS
323 SELECT * FROM pg_statio_all_sequences
324 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND
325 schemaname !~ '^pg_toast';
327 CREATE VIEW pg_stat_activity AS
330 D.datname AS datname,
333 U.rolname AS usename,
341 FROM pg_database D, pg_stat_get_activity(NULL) AS S, pg_authid U
342 WHERE S.datid = D.oid AND
345 CREATE VIEW pg_stat_database AS
348 D.datname AS datname,
349 pg_stat_get_db_numbackends(D.oid) AS numbackends,
350 pg_stat_get_db_xact_commit(D.oid) AS xact_commit,
351 pg_stat_get_db_xact_rollback(D.oid) AS xact_rollback,
352 pg_stat_get_db_blocks_fetched(D.oid) -
353 pg_stat_get_db_blocks_hit(D.oid) AS blks_read,
354 pg_stat_get_db_blocks_hit(D.oid) AS blks_hit,
355 pg_stat_get_db_tuples_returned(D.oid) AS tup_returned,
356 pg_stat_get_db_tuples_fetched(D.oid) AS tup_fetched,
357 pg_stat_get_db_tuples_inserted(D.oid) AS tup_inserted,
358 pg_stat_get_db_tuples_updated(D.oid) AS tup_updated,
359 pg_stat_get_db_tuples_deleted(D.oid) AS tup_deleted
362 CREATE VIEW pg_stat_user_functions AS
365 N.nspname AS schemaname,
366 P.proname AS funcname,
367 pg_stat_get_function_calls(P.oid) AS calls,
368 pg_stat_get_function_time(P.oid) / 1000 AS total_time,
369 pg_stat_get_function_self_time(P.oid) / 1000 AS self_time
370 FROM pg_proc P LEFT JOIN pg_namespace N ON (N.oid = P.pronamespace)
371 WHERE P.prolang != 12 -- fast check to eliminate built-in functions
372 AND pg_stat_get_function_calls(P.oid) IS NOT NULL;
374 CREATE VIEW pg_stat_bgwriter AS
376 pg_stat_get_bgwriter_timed_checkpoints() AS checkpoints_timed,
377 pg_stat_get_bgwriter_requested_checkpoints() AS checkpoints_req,
378 pg_stat_get_bgwriter_buf_written_checkpoints() AS buffers_checkpoint,
379 pg_stat_get_bgwriter_buf_written_clean() AS buffers_clean,
380 pg_stat_get_bgwriter_maxwritten_clean() AS maxwritten_clean,
381 pg_stat_get_buf_written_backend() AS buffers_backend,
382 pg_stat_get_buf_alloc() AS buffers_alloc;
384 CREATE VIEW pg_user_mappings AS
388 S.srvname AS srvname,
390 CASE WHEN U.umuser = 0 THEN
395 CASE WHEN pg_has_role(S.srvowner, 'USAGE') OR has_server_privilege(S.oid, 'USAGE') THEN
400 FROM pg_user_mapping U
401 LEFT JOIN pg_authid A ON (A.oid = U.umuser) JOIN
402 pg_foreign_server S ON (U.umserver = S.oid);
404 REVOKE ALL on pg_user_mapping FROM public;
407 -- We have a few function definitions in here, too.
408 -- At some point there might be enough to justify breaking them out into
409 -- a separate "system_functions.sql" file.
412 -- Tsearch debug function. Defined here because it'd be pretty unwieldy
413 -- to put it into pg_proc.h
415 CREATE FUNCTION ts_debug(IN config regconfig, IN document text,
417 OUT description text,
419 OUT dictionaries regdictionary[],
420 OUT dictionary regdictionary,
422 RETURNS SETOF record AS
426 tt.description AS description,
427 parse.token AS token,
428 ARRAY ( SELECT m.mapdict::pg_catalog.regdictionary
429 FROM pg_catalog.pg_ts_config_map AS m
430 WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
431 ORDER BY m.mapseqno )
433 ( SELECT mapdict::pg_catalog.regdictionary
434 FROM pg_catalog.pg_ts_config_map AS m
435 WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
436 ORDER BY pg_catalog.ts_lexize(mapdict, parse.token) IS NULL, m.mapseqno
439 ( SELECT pg_catalog.ts_lexize(mapdict, parse.token)
440 FROM pg_catalog.pg_ts_config_map AS m
441 WHERE m.mapcfg = $1 AND m.maptokentype = parse.tokid
442 ORDER BY pg_catalog.ts_lexize(mapdict, parse.token) IS NULL, m.mapseqno
445 FROM pg_catalog.ts_parse(
446 (SELECT cfgparser FROM pg_catalog.pg_ts_config WHERE oid = $1 ), $2
448 pg_catalog.ts_token_type(
449 (SELECT cfgparser FROM pg_catalog.pg_ts_config WHERE oid = $1 )
451 WHERE tt.tokid = parse.tokid
453 LANGUAGE SQL STRICT STABLE;
455 COMMENT ON FUNCTION ts_debug(regconfig,text) IS
456 'debug function for text search configuration';
458 CREATE FUNCTION ts_debug(IN document text,
460 OUT description text,
462 OUT dictionaries regdictionary[],
463 OUT dictionary regdictionary,
465 RETURNS SETOF record AS
467 SELECT * FROM pg_catalog.ts_debug( pg_catalog.get_current_ts_config(), $1);
469 LANGUAGE SQL STRICT STABLE;
471 COMMENT ON FUNCTION ts_debug(text) IS
472 'debug function for current text search configuration';
475 -- Redeclare built-in functions that need default values attached to their
476 -- arguments. It's impractical to set those up directly in pg_proc.h because
477 -- of the complexity and platform-dependency of the expression tree
478 -- representation. (Note that internal functions still have to have entries
479 -- in pg_proc.h; we are merely causing their proargnames and proargdefaults
480 -- to get filled in.)
483 CREATE OR REPLACE FUNCTION
484 pg_start_backup(label text, fast boolean DEFAULT false)
485 RETURNS text STRICT VOLATILE LANGUAGE internal AS 'pg_start_backup';