Commit 267a8f82 authored by Tom Lane's avatar Tom Lane

Change SQL commands embedded in the initdb script from the style

	echo "command" | postgres
to the style
	postgres <<EOF
		command
	EOF
This makes the script more legible (IMHO anyway) by reducing the need
to escape quotes, and allows us to execute successive SQL commands in
a single standalone-backend run, rather than needing to start a new
standalone backend for each command.  With all the CREATE VIEWs that
are getting done now, this makes for a rather substantial reduction
in the runtime of initdb.  (Some of us do initdb often enough to care
how long it runs ;-).)
parent 9c2b1a92
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
# Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group # Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California # Portions Copyright (c) 1994, Regents of the University of California
# #
# $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.130 2001/07/15 22:48:18 tgl Exp $ # $Header: /cvsroot/pgsql/src/bin/initdb/Attic/initdb.sh,v 1.131 2001/07/31 01:16:09 tgl Exp $
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
...@@ -478,21 +478,28 @@ chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \ ...@@ -478,21 +478,28 @@ chmod 0600 "$PGDATA"/pg_hba.conf "$PGDATA"/pg_ident.conf \
########################################################################## ##########################################################################
# #
# CREATE VIEWS and other things # CREATE VIEWS and other things
#
# NOTE: because here we are driving a standalone backend (not psql), we must
# follow the standalone backend's convention that commands end at a newline.
# To break an SQL command across lines in this script, backslash-escape all
# internal newlines in the command.
echo "Initializing pg_shadow." echo "Initializing pg_shadow."
PGSQL_OPT="-o /dev/null -O -F -D$PGDATA" PGSQL_OPT="-o /dev/null -O -F -D$PGDATA"
# Create a trigger so that direct updates to pg_shadow will be written "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
# to the flat password file pg_pwd -- Create a trigger so that direct updates to pg_shadow will be written
echo "CREATE TRIGGER pg_sync_pg_pwd AFTER INSERT OR UPDATE OR DELETE ON pg_shadow" \ -- to the flat password file pg_pwd
"FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd()" \ CREATE TRIGGER pg_sync_pg_pwd AFTER INSERT OR UPDATE OR DELETE ON pg_shadow \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely FOR EACH ROW EXECUTE PROCEDURE update_pg_pwd();
-- needs to be done before alter user, because alter user checks that
# needs to be done before alter user, because alter user checks that -- pg_shadow is secure ...
# pg_shadow is secure ... REVOKE ALL on pg_shadow FROM public;
echo "REVOKE ALL on pg_shadow FROM public" \ EOF
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely if [ "$?" -ne 0 ]; then
exit_nicely
fi
# set up password # set up password
if [ "$PwPrompt" ]; then if [ "$PwPrompt" ]; then
...@@ -510,8 +517,12 @@ if [ "$PwPrompt" ]; then ...@@ -510,8 +517,12 @@ if [ "$PwPrompt" ]; then
echo "Passwords didn't match." 1>&2 echo "Passwords didn't match." 1>&2
exit_nicely exit_nicely
fi fi
echo "ALTER USER \"$POSTGRES_SUPERUSERNAME\" WITH PASSWORD '$FirstPw'" \ "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely ALTER USER "$POSTGRES_SUPERUSERNAME" WITH PASSWORD '$FirstPw';
EOF
if [ "$?" -ne 0 ]; then
exit_nicely
fi
if [ ! -f $PGDATA/global/pg_pwd ]; then if [ ! -f $PGDATA/global/pg_pwd ]; then
echo "The password file wasn't generated. Please report this problem." 1>&2 echo "The password file wasn't generated. Please report this problem." 1>&2
exit_nicely exit_nicely
...@@ -522,281 +533,263 @@ fi ...@@ -522,281 +533,263 @@ fi
echo "Enabling unlimited row width for system tables." echo "Enabling unlimited row width for system tables."
echo "ALTER TABLE pg_attrdef CREATE TOAST TABLE" \ "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely ALTER TABLE pg_attrdef CREATE TOAST TABLE;
echo "ALTER TABLE pg_description CREATE TOAST TABLE" \ ALTER TABLE pg_description CREATE TOAST TABLE;
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely ALTER TABLE pg_proc CREATE TOAST TABLE;
echo "ALTER TABLE pg_proc CREATE TOAST TABLE" \ ALTER TABLE pg_relcheck CREATE TOAST TABLE;
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely ALTER TABLE pg_rewrite CREATE TOAST TABLE;
echo "ALTER TABLE pg_relcheck CREATE TOAST TABLE" \ ALTER TABLE pg_statistic CREATE TOAST TABLE;
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely EOF
echo "ALTER TABLE pg_rewrite CREATE TOAST TABLE" \ if [ "$?" -ne 0 ]; then
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely exit_nicely
echo "ALTER TABLE pg_statistic CREATE TOAST TABLE" \ fi
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "Creating system views." echo "Creating system views."
echo "CREATE VIEW pg_user AS \ "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
SELECT \
usename, \ CREATE VIEW pg_user AS \
usesysid, \ SELECT \
usecreatedb, \ usename, \
usetrace, \ usesysid, \
usesuper, \ usecreatedb, \
usecatupd, \ usetrace, \
'********'::text as passwd, \ usesuper, \
valuntil \ usecatupd, \
FROM pg_shadow" \ '********'::text as passwd, \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely valuntil \
FROM pg_shadow;
echo "CREATE VIEW pg_rules AS \
SELECT \ CREATE VIEW pg_rules AS \
C.relname AS tablename, \ SELECT \
R.rulename AS rulename, \ C.relname AS tablename, \
pg_get_ruledef(R.rulename) AS definition \ R.rulename AS rulename, \
FROM pg_rewrite R, pg_class C \ pg_get_ruledef(R.rulename) AS definition \
WHERE R.rulename !~ '^_RET' \ FROM pg_rewrite R, pg_class C \
AND C.oid = R.ev_class;" \ WHERE R.rulename !~ '^_RET' \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely AND C.oid = R.ev_class;
echo "CREATE VIEW pg_views AS \ CREATE VIEW pg_views AS \
SELECT \ SELECT \
C.relname AS viewname, \ C.relname AS viewname, \
pg_get_userbyid(C.relowner) AS viewowner, \ pg_get_userbyid(C.relowner) AS viewowner, \
pg_get_viewdef(C.relname) AS definition \ pg_get_viewdef(C.relname) AS definition \
FROM pg_class C \ FROM pg_class C \
WHERE C.relkind = 'v';" \ WHERE C.relkind = 'v';
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
-- XXX why does pg_tables include sequences?
# XXX why does pg_tables include sequences?
CREATE VIEW pg_tables AS \
echo "CREATE VIEW pg_tables AS \ SELECT \
SELECT \ C.relname AS tablename, \
C.relname AS tablename, \ pg_get_userbyid(C.relowner) AS tableowner, \
pg_get_userbyid(C.relowner) AS tableowner, \ C.relhasindex AS hasindexes, \
C.relhasindex AS hasindexes, \ C.relhasrules AS hasrules, \
C.relhasrules AS hasrules, \ (C.reltriggers > 0) AS hastriggers \
(C.reltriggers > 0) AS hastriggers \ FROM pg_class C \
FROM pg_class C \ WHERE C.relkind IN ('r', 's');
WHERE C.relkind IN ('r', 's');" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely CREATE VIEW pg_indexes AS \
SELECT \
echo "CREATE VIEW pg_indexes AS \ C.relname AS tablename, \
SELECT \ I.relname AS indexname, \
C.relname AS tablename, \ pg_get_indexdef(X.indexrelid) AS indexdef \
I.relname AS indexname, \ FROM pg_index X, pg_class C, pg_class I \
pg_get_indexdef(X.indexrelid) AS indexdef \ WHERE C.relkind = 'r' AND I.relkind = 'i' \
FROM pg_index X, pg_class C, pg_class I \ AND C.oid = X.indrelid \
WHERE C.relkind = 'r' AND I.relkind = 'i' \ AND I.oid = X.indexrelid;
AND C.oid = X.indrelid \
AND I.oid = X.indexrelid;" \ CREATE VIEW pg_stats AS \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely SELECT \
relname AS tablename, \
echo "CREATE VIEW pg_stats AS \ attname AS attname, \
SELECT \ stanullfrac AS null_frac, \
relname AS tablename, \ stawidth AS avg_width, \
attname AS attname, \ stadistinct AS n_distinct, \
stanullfrac AS null_frac, \ CASE 1 \
stawidth AS avg_width, \ WHEN stakind1 THEN stavalues1 \
stadistinct AS n_distinct, \ WHEN stakind2 THEN stavalues2 \
CASE 1 \ WHEN stakind3 THEN stavalues3 \
WHEN stakind1 THEN stavalues1 \ WHEN stakind4 THEN stavalues4 \
WHEN stakind2 THEN stavalues2 \ END AS most_common_vals, \
WHEN stakind3 THEN stavalues3 \ CASE 1 \
WHEN stakind4 THEN stavalues4 \ WHEN stakind1 THEN stanumbers1 \
END AS most_common_vals, \ WHEN stakind2 THEN stanumbers2 \
CASE 1 \ WHEN stakind3 THEN stanumbers3 \
WHEN stakind1 THEN stanumbers1 \ WHEN stakind4 THEN stanumbers4 \
WHEN stakind2 THEN stanumbers2 \ END AS most_common_freqs, \
WHEN stakind3 THEN stanumbers3 \ CASE 2 \
WHEN stakind4 THEN stanumbers4 \ WHEN stakind1 THEN stavalues1 \
END AS most_common_freqs, \ WHEN stakind2 THEN stavalues2 \
CASE 2 \ WHEN stakind3 THEN stavalues3 \
WHEN stakind1 THEN stavalues1 \ WHEN stakind4 THEN stavalues4 \
WHEN stakind2 THEN stavalues2 \ END AS histogram_bounds, \
WHEN stakind3 THEN stavalues3 \ CASE 3 \
WHEN stakind4 THEN stavalues4 \ WHEN stakind1 THEN stanumbers1[1] \
END AS histogram_bounds, \ WHEN stakind2 THEN stanumbers2[1] \
CASE 3 \ WHEN stakind3 THEN stanumbers3[1] \
WHEN stakind1 THEN stanumbers1[1] \ WHEN stakind4 THEN stanumbers4[1] \
WHEN stakind2 THEN stanumbers2[1] \ END AS correlation \
WHEN stakind3 THEN stanumbers3[1] \ FROM pg_class c, pg_attribute a, pg_statistic s \
WHEN stakind4 THEN stanumbers4[1] \ WHERE c.oid = s.starelid AND c.oid = a.attrelid \
END AS correlation \ AND a.attnum = s.staattnum \
FROM pg_class c, pg_attribute a, pg_statistic s \ AND has_table_privilege(c.oid, 'select');
WHERE c.oid = s.starelid AND c.oid = a.attrelid \
AND a.attnum = s.staattnum \ REVOKE ALL on pg_statistic FROM public;
AND has_table_privilege(c.oid, 'select');" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely CREATE VIEW pg_stat_all_tables AS \
SELECT \
echo "REVOKE ALL on pg_statistic FROM public" \ C.oid AS relid, \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely C.relname AS relname, \
pg_stat_get_numscans(C.oid) AS seq_scan, \
echo "CREATE VIEW pg_stat_all_tables AS \ pg_stat_get_tuples_returned(C.oid) AS seq_tup_read, \
SELECT \ sum(pg_stat_get_numscans(I.indexrelid)) AS idx_scan, \
C.oid AS relid, \ sum(pg_stat_get_tuples_fetched(I.indexrelid)) AS idx_tup_fetch, \
C.relname AS relname, \ pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins, \
pg_stat_get_numscans(C.oid) AS seq_scan, \ pg_stat_get_tuples_updated(C.oid) AS n_tup_upd, \
pg_stat_get_tuples_returned(C.oid) AS seq_tup_read, \ pg_stat_get_tuples_deleted(C.oid) AS n_tup_del \
sum(pg_stat_get_numscans(I.indexrelid)) AS idx_scan, \ FROM pg_class C FULL OUTER JOIN \
sum(pg_stat_get_tuples_fetched(I.indexrelid)) AS idx_tup_fetch, \ pg_index I ON C.oid = I.indrelid \
pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins, \ WHERE C.relkind = 'r' \
pg_stat_get_tuples_updated(C.oid) AS n_tup_upd, \ GROUP BY C.oid, C.relname;
pg_stat_get_tuples_deleted(C.oid) AS n_tup_del \
FROM pg_class C FULL OUTER JOIN \ CREATE VIEW pg_stat_sys_tables AS \
pg_index I ON C.oid = I.indrelid \ SELECT * FROM pg_stat_all_tables \
WHERE C.relkind = 'r' \ WHERE relname ~ '^pg_';
GROUP BY C.oid, C.relname;" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely CREATE VIEW pg_stat_user_tables AS \
SELECT * FROM pg_stat_all_tables \
echo "CREATE VIEW pg_stat_sys_tables AS \ WHERE relname !~ '^pg_';
SELECT * FROM pg_stat_all_tables \
WHERE relname ~ '^pg_';" \ CREATE VIEW pg_statio_all_tables AS \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely SELECT \
C.oid AS relid, \
echo "CREATE VIEW pg_stat_user_tables AS \ C.relname AS relname, \
SELECT * FROM pg_stat_all_tables \ pg_stat_get_blocks_fetched(C.oid) - \
WHERE relname !~ '^pg_';" \ pg_stat_get_blocks_hit(C.oid) AS heap_blks_read, \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely pg_stat_get_blocks_hit(C.oid) AS heap_blks_hit, \
sum(pg_stat_get_blocks_fetched(I.indexrelid) - \
echo "CREATE VIEW pg_statio_all_tables AS \ pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_read, \
SELECT \ sum(pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_hit, \
C.oid AS relid, \ pg_stat_get_blocks_fetched(T.oid) - \
C.relname AS relname, \ pg_stat_get_blocks_hit(T.oid) AS toast_blks_read, \
pg_stat_get_blocks_fetched(C.oid) - \ pg_stat_get_blocks_hit(T.oid) AS toast_blks_hit, \
pg_stat_get_blocks_hit(C.oid) AS heap_blks_read, \ pg_stat_get_blocks_fetched(X.oid) - \
pg_stat_get_blocks_hit(C.oid) AS heap_blks_hit, \ pg_stat_get_blocks_hit(X.oid) AS tidx_blks_read, \
sum(pg_stat_get_blocks_fetched(I.indexrelid) - \ pg_stat_get_blocks_hit(X.oid) AS tidx_blks_hit \
pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_read, \ FROM pg_class C FULL OUTER JOIN \
sum(pg_stat_get_blocks_hit(I.indexrelid)) AS idx_blks_hit, \ pg_index I ON C.oid = I.indrelid FULL OUTER JOIN \
pg_stat_get_blocks_fetched(T.oid) - \ pg_class T ON C.reltoastrelid = T.oid FULL OUTER JOIN \
pg_stat_get_blocks_hit(T.oid) AS toast_blks_read, \ pg_class X ON C.reltoastidxid = X.oid \
pg_stat_get_blocks_hit(T.oid) AS toast_blks_hit, \ WHERE C.relkind = 'r' \
pg_stat_get_blocks_fetched(X.oid) - \ GROUP BY C.oid, C.relname, T.oid, X.oid;
pg_stat_get_blocks_hit(X.oid) AS tidx_blks_read, \
pg_stat_get_blocks_hit(X.oid) AS tidx_blks_hit \ CREATE VIEW pg_statio_sys_tables AS \
FROM pg_class C FULL OUTER JOIN \ SELECT * FROM pg_statio_all_tables \
pg_index I ON C.oid = I.indrelid FULL OUTER JOIN \ WHERE relname ~ '^pg_';
pg_class T ON C.reltoastrelid = T.oid FULL OUTER JOIN \
pg_class X ON C.reltoastidxid = X.oid \ CREATE VIEW pg_statio_user_tables AS \
WHERE C.relkind = 'r' \ SELECT * FROM pg_statio_all_tables \
GROUP BY C.oid, C.relname, T.oid, X.oid;" \ WHERE relname !~ '^pg_';
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
CREATE VIEW pg_stat_all_indexes AS \
echo "CREATE VIEW pg_statio_sys_tables AS \ SELECT \
SELECT * FROM pg_statio_all_tables \ C.oid AS relid, \
WHERE relname ~ '^pg_';" \ I.oid AS indexrelid, \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely C.relname AS relname, \
I.relname AS indexrelname, \
echo "CREATE VIEW pg_statio_user_tables AS \ pg_stat_get_numscans(I.oid) AS idx_scan, \
SELECT * FROM pg_statio_all_tables \ pg_stat_get_tuples_returned(I.oid) AS idx_tup_read, \
WHERE relname !~ '^pg_';" \ pg_stat_get_tuples_fetched(I.oid) AS idx_tup_fetch \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely FROM pg_class C, \
pg_class I, \
echo "CREATE VIEW pg_stat_all_indexes AS \ pg_index X \
SELECT \ WHERE C.relkind = 'r' AND \
C.oid AS relid, \ X.indrelid = C.oid AND \
I.oid AS indexrelid, \ X.indexrelid = I.oid;
C.relname AS relname, \
I.relname AS indexrelname, \ CREATE VIEW pg_stat_sys_indexes AS \
pg_stat_get_numscans(I.oid) AS idx_scan, \ SELECT * FROM pg_stat_all_indexes \
pg_stat_get_tuples_returned(I.oid) AS idx_tup_read, \ WHERE relname ~ '^pg_';
pg_stat_get_tuples_fetched(I.oid) AS idx_tup_fetch \
FROM pg_class C, \ CREATE VIEW pg_stat_user_indexes AS \
pg_class I, \ SELECT * FROM pg_stat_all_indexes \
pg_index X \ WHERE relname !~ '^pg_';
WHERE C.relkind = 'r' AND \
X.indrelid = C.oid AND \ CREATE VIEW pg_statio_all_indexes AS \
X.indexrelid = I.oid;" \ SELECT \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely C.oid AS relid, \
I.oid AS indexrelid, \
echo "CREATE VIEW pg_stat_sys_indexes AS \ C.relname AS relname, \
SELECT * FROM pg_stat_all_indexes \ I.relname AS indexrelname, \
WHERE relname ~ '^pg_';" \ pg_stat_get_blocks_fetched(I.oid) - \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely pg_stat_get_blocks_hit(I.oid) AS idx_blks_read, \
pg_stat_get_blocks_hit(I.oid) AS idx_blks_hit \
echo "CREATE VIEW pg_stat_user_indexes AS \ FROM pg_class C, \
SELECT * FROM pg_stat_all_indexes \ pg_class I, \
WHERE relname !~ '^pg_';" \ pg_index X \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely WHERE C.relkind = 'r' AND \
X.indrelid = C.oid AND \
echo "CREATE VIEW pg_statio_all_indexes AS \ X.indexrelid = I.oid;
SELECT \
C.oid AS relid, \ CREATE VIEW pg_statio_sys_indexes AS \
I.oid AS indexrelid, \ SELECT * FROM pg_statio_all_indexes \
C.relname AS relname, \ WHERE relname ~ '^pg_';
I.relname AS indexrelname, \
pg_stat_get_blocks_fetched(I.oid) - \ CREATE VIEW pg_statio_user_indexes AS \
pg_stat_get_blocks_hit(I.oid) AS idx_blks_read, \ SELECT * FROM pg_statio_all_indexes \
pg_stat_get_blocks_hit(I.oid) AS idx_blks_hit \ WHERE relname !~ '^pg_';
FROM pg_class C, \
pg_class I, \ CREATE VIEW pg_statio_all_sequences AS \
pg_index X \ SELECT \
WHERE C.relkind = 'r' AND \ C.oid AS relid, \
X.indrelid = C.oid AND \ C.relname AS relname, \
X.indexrelid = I.oid;" \ pg_stat_get_blocks_fetched(C.oid) - \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely pg_stat_get_blocks_hit(C.oid) AS blks_read, \
pg_stat_get_blocks_hit(C.oid) AS blks_hit \
echo "CREATE VIEW pg_statio_sys_indexes AS \ FROM pg_class C \
SELECT * FROM pg_statio_all_indexes \ WHERE C.relkind = 'S';
WHERE relname ~ '^pg_';" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely CREATE VIEW pg_statio_sys_sequences AS \
SELECT * FROM pg_statio_all_sequences \
echo "CREATE VIEW pg_statio_user_indexes AS \ WHERE relname ~ '^pg_';
SELECT * FROM pg_statio_all_indexes \
WHERE relname !~ '^pg_';" \ CREATE VIEW pg_statio_user_sequences AS \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely SELECT * FROM pg_statio_all_sequences \
WHERE relname !~ '^pg_';
echo "CREATE VIEW pg_statio_all_sequences AS \
SELECT \ CREATE VIEW pg_stat_activity AS \
C.oid AS relid, \ SELECT \
C.relname AS relname, \ D.oid AS datid, \
pg_stat_get_blocks_fetched(C.oid) - \ D.datname AS datname, \
pg_stat_get_blocks_hit(C.oid) AS blks_read, \ pg_stat_get_backend_pid(S.backendid) AS procpid, \
pg_stat_get_blocks_hit(C.oid) AS blks_hit \ pg_stat_get_backend_userid(S.backendid) AS usesysid, \
FROM pg_class C \ U.usename AS usename, \
WHERE C.relkind = 'S';" \ pg_stat_get_backend_activity(S.backendid) AS current_query \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely FROM pg_database D, \
(SELECT pg_stat_get_backend_idset() AS backendid) AS S, \
echo "CREATE VIEW pg_statio_sys_sequences AS \ pg_shadow U \
SELECT * FROM pg_statio_all_sequences \ WHERE pg_stat_get_backend_dbid(S.backendid) = D.oid AND \
WHERE relname ~ '^pg_';" \ pg_stat_get_backend_userid(S.backendid) = U.usesysid;
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
CREATE VIEW pg_stat_database AS \
echo "CREATE VIEW pg_statio_user_sequences AS \ SELECT \
SELECT * FROM pg_statio_all_sequences \ D.oid AS datid, \
WHERE relname !~ '^pg_';" \ D.datname AS datname, \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely pg_stat_get_db_numbackends(D.oid) AS numbackends, \
pg_stat_get_db_xact_commit(D.oid) AS xact_commit, \
echo "CREATE VIEW pg_stat_activity AS \ pg_stat_get_db_xact_rollback(D.oid) AS xact_rollback, \
SELECT \ pg_stat_get_db_blocks_fetched(D.oid) - \
D.oid AS datid, \ pg_stat_get_db_blocks_hit(D.oid) AS blks_read, \
D.datname AS datname, \ pg_stat_get_db_blocks_hit(D.oid) AS blks_hit \
pg_stat_get_backend_pid(S.backendid) AS procpid, \ FROM pg_database D;
pg_stat_get_backend_userid(S.backendid) AS usesysid, \
U.usename AS usename, \ EOF
pg_stat_get_backend_activity(S.backendid) AS current_query \ if [ "$?" -ne 0 ]; then
FROM pg_database D, \ exit_nicely
(SELECT pg_stat_get_backend_idset() AS backendid) AS S, \ fi
pg_shadow U \
WHERE pg_stat_get_backend_dbid(S.backendid) = D.oid AND \
pg_stat_get_backend_userid(S.backendid) = U.usesysid;" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "CREATE VIEW pg_stat_database AS \
SELECT \
D.oid AS datid, \
D.datname AS datname, \
pg_stat_get_db_numbackends(D.oid) AS numbackends, \
pg_stat_get_db_xact_commit(D.oid) AS xact_commit, \
pg_stat_get_db_xact_rollback(D.oid) AS xact_rollback, \
pg_stat_get_db_blocks_fetched(D.oid) - \
pg_stat_get_db_blocks_hit(D.oid) AS blks_read, \
pg_stat_get_db_blocks_hit(D.oid) AS blks_hit \
FROM pg_database D;" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "Loading pg_description." echo "Loading pg_description."
( (
...@@ -806,26 +799,39 @@ echo "Loading pg_description." ...@@ -806,26 +799,39 @@ echo "Loading pg_description."
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely | "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
echo "Setting lastsysoid." echo "Setting lastsysoid."
echo "UPDATE pg_database SET \
"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
UPDATE pg_database SET \
datistemplate = 't', \ datistemplate = 't', \
datlastsysoid = (SELECT max(oid) FROM pg_description) \ datlastsysoid = (SELECT max(oid) FROM pg_description) \
WHERE datname = 'template1'" \ WHERE datname = 'template1';
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely EOF
if [ "$?" -ne 0 ]; then
exit_nicely
fi
echo "Vacuuming database." echo "Vacuuming database."
echo "VACUUM FULL ANALYZE" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
VACUUM FULL ANALYZE;
EOF
if [ "$?" -ne 0 ]; then
exit_nicely
fi
echo "Copying template1 to template0." echo "Copying template1 to template0."
echo "CREATE DATABASE template0" \
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <<EOF
echo "UPDATE pg_database SET \ CREATE DATABASE template0;
UPDATE pg_database SET \
datistemplate = 't', \ datistemplate = 't', \
datallowconn = 'f' \ datallowconn = 'f' \
WHERE datname = 'template0'" \ WHERE datname = 'template0';
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely VACUUM FULL pg_database;
echo "VACUUM FULL pg_database" \ EOF
| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely if [ "$?" -ne 0 ]; then
exit_nicely
fi
########################################################################## ##########################################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment