*** ./doc/src/sgml/func.sgml.orig 2004-12-16 23:52:04.000000000 -0200 --- ./doc/src/sgml/func.sgml 2004-12-17 00:02:33.000000000 -0200 *************** *** 8003,8008 **** --- 8003,8014 ---- + uptime() + text + PostgreSQL uptime information + + + user name equivalent to current_user *************** *** 8099,8104 **** --- 8105,8119 ---- + uptime + + + + uptime() returns the PostgreSQL + server's uptime. + + + version *** ./src/backend/postmaster/postmaster.c.orig 2004-12-17 00:20:51.000000000 -0200 --- ./src/backend/postmaster/postmaster.c 2004-12-17 00:23:14.000000000 -0200 *************** *** 914,919 **** --- 914,924 ---- */ StartupPID = StartupDataBase(); + /* + * Get time so we can use it to calculate PostgreSQL server uptime + */ + PostmasterStartupTime = time(NULL); + status = ServerLoop(); /* *** ./src/backend/utils/adt/Makefile.orig 2004-12-16 23:50:14.000000000 -0200 --- ./src/backend/utils/adt/Makefile 2004-12-16 23:51:01.000000000 -0200 *************** *** 24,30 **** tid.o timestamp.o varbit.o varchar.o varlena.o version.o xid.o \ network.o mac.o inet_net_ntop.o inet_net_pton.o \ ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o \ ! ascii.o quote.o pgstatfuncs.o encode.o like.o: like.c like_match.c --- 24,30 ---- tid.o timestamp.o varbit.o varchar.o varlena.o version.o xid.o \ network.o mac.o inet_net_ntop.o inet_net_pton.o \ ri_triggers.o pg_lzcompress.o pg_locale.o formatting.o \ ! ascii.o quote.o pgstatfuncs.o encode.o uptime.o like.o: like.c like_match.c *** ./src/backend/utils/adt/uptime.c.orig 2004-12-17 09:42:31.000000000 -0200 --- ./src/backend/utils/adt/uptime.c 2004-12-17 09:52:59.000000000 -0200 *************** *** 0 **** --- 1,86 ---- + /*------------------------------------------------------------------------- + * + * uptime.c + * + * Returns the PostgreSQL server uptime information + * + * Portions Copyright (c) 2004, PostgreSQL Global Development Group + * + * + * IDENTIFICATION + * + * $PostgreSQL$ + * + *------------------------------------------------------------------------- + */ + + #include "postgres.h" + + #include + + #include "miscadmin.h" + #include "utils/builtins.h" + + + Datum + pgsql_uptime(PG_FUNCTION_ARGS) + { + char buf[64]; + int buflen; + time_t now; + long updays, uphours, upmins, upsecs; + text *result; + + if (PostmasterStartupTime == InvalidTime) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + (errmsg("invalid value for PostmasterStartupTime")))); + + /* calculate the uptime */ + now = time(NULL); + upsecs = now - PostmasterStartupTime; + + /* days */ + updays = (long) (upsecs / (60 * 60 * 24)); + if (updays) + snprintf(buf, sizeof(buf), + " %ld %s", updays, (updays == 1) ? "day" : "days"); + + /* + * strlen(buf) report one if the buf is empty, but we need + * to start at zero. Repeat it until seconds. + */ + buflen = (strlen(buf) > 1) ? strlen(buf) : strlen(buf) - 1; + + /* hours */ + upsecs %= (60 * 60 * 24); + uphours = (long) (upsecs / (60 * 60)); + if (uphours) + snprintf(buf + buflen, sizeof(buf) - buflen, + " %ld %s", uphours, (uphours == 1) ? "hour" : "hours"); + + buflen = (strlen(buf) > 1) ? strlen(buf) : strlen(buf) - 1; + + /* minutes */ + upsecs %= (60 * 60); + upmins = (long) (upsecs / 60); + if (upmins) + snprintf(buf + buflen, sizeof(buf) - buflen, + " %ld %s", upmins, (upmins == 1) ? "minute" : "minutes"); + + buflen = (strlen(buf) > 1) ? strlen(buf) : strlen(buf) - 1; + + /* seconds */ + upsecs %= 60; + if (upsecs) + snprintf(buf + buflen, sizeof(buf) - buflen, + " %ld %s", upsecs, (upsecs == 1) ? "second" : "seconds"); + + /* make a text type for output */ + buflen = strlen(buf); + result = (text *) palloc(buflen + VARHDRSZ); + VARATT_SIZEP(result) = buflen + VARHDRSZ; + memcpy(VARDATA(result), buf, buflen); + + PG_RETURN_TEXT_P(result); + } *** ./src/backend/utils/init/globals.c.orig 2004-12-17 00:14:56.000000000 -0200 --- ./src/backend/utils/init/globals.c 2004-12-17 00:16:25.000000000 -0200 *************** *** 63,68 **** --- 63,70 ---- pid_t PostmasterPid = 0; + time_t PostmasterStartupTime = InvalidTime; + /* * IsPostmasterEnvironment is true in a postmaster process and any postmaster * child process; it is false in a standalone process (bootstrap or *** ./src/include/catalog/pg_proc.h.orig 2004-12-17 00:10:41.000000000 -0200 --- ./src/include/catalog/pg_proc.h 2004-12-17 00:13:06.000000000 -0200 *************** *** 199,204 **** --- 199,206 ---- DATA(insert OID = 84 ( boolne PGNSP PGUID 12 f f t f i 2 16 "16 16" _null_ boolne - _null_ )); DESCR("not equal"); + DATA(insert OID = 88 ( uptime PGNSP PGUID 12 f f t f s 0 25 "" _null_ pgsql_uptime - _null_ )); + DESCR("PostgreSQL server uptime"); DATA(insert OID = 89 ( version PGNSP PGUID 12 f f t f s 0 25 "" _null_ pgsql_version - _null_ )); DESCR("PostgreSQL version string"); *** ./src/include/miscadmin.h.orig 2004-12-17 09:10:53.000000000 -0200 --- ./src/include/miscadmin.h 2004-12-17 00:20:32.000000000 -0200 *************** *** 117,126 **** --- 117,129 ---- * globals.h -- * *****************************************************************************/ + #define InvalidTime (-1) + /* * from utils/init/globals.c */ extern pid_t PostmasterPid; + extern time_t PostmasterStartupTime; extern bool IsPostmasterEnvironment; extern bool IsUnderPostmaster; *** ./src/include/utils/builtins.h.orig 2004-12-17 00:13:36.000000000 -0200 --- ./src/include/utils/builtins.h 2004-12-17 00:14:07.000000000 -0200 *************** *** 595,600 **** --- 595,603 ---- extern Datum bytea_substr(PG_FUNCTION_ARGS); extern Datum bytea_substr_no_len(PG_FUNCTION_ARGS); + /* uptime.c */ + extern Datum pgsql_uptime(PG_FUNCTION_ARGS); + /* version.c */ extern Datum pgsql_version(PG_FUNCTION_ARGS);