diff --git a/src/backend/catalog/system_views.sql b/src/backend/catalog/system_views.sql index 2253ca8..55d20dc 100644 --- a/src/backend/catalog/system_views.sql +++ b/src/backend/catalog/system_views.sql @@ -574,6 +574,8 @@ CREATE VIEW pg_stat_database AS pg_stat_get_db_tuples_updated(D.oid) AS tup_updated, pg_stat_get_db_tuples_deleted(D.oid) AS tup_deleted, pg_stat_get_db_conflict_all(D.oid) AS conflicts, + pg_stat_get_db_temp_files(D.oid) AS temp_files, + pg_stat_get_db_temp_bytes(D.oid) AS temp_bytes, pg_stat_get_db_stat_reset_time(D.oid) AS stats_reset FROM pg_database D; diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 24f4cde..97c7004 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -286,7 +286,7 @@ static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len); static void pgstat_recv_funcstat(PgStat_MsgFuncstat *msg, int len); static void pgstat_recv_funcpurge(PgStat_MsgFuncpurge *msg, int len); static void pgstat_recv_recoveryconflict(PgStat_MsgRecoveryConflict *msg, int len); - +static void pgstat_recv_tempfile(PgStat_MsgTempFile *msg, int len); /* ------------------------------------------------------------ * Public functions called from postmaster follow @@ -1339,6 +1339,29 @@ pgstat_report_recovery_conflict(int reason) pgstat_send(&msg, sizeof(msg)); } + +/* -------- + * pgstat_report_tempfile() - + * + * Tell the collector about a temporary file. + * -------- + */ +void +pgstat_report_tempfile(size_t filesize) +{ + PgStat_MsgTempFile msg; + + if (pgStatSock == PGINVALID_SOCKET || !pgstat_track_counts) + return; + + pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TEMPFILE); + msg.m_databaseid = MyDatabaseId; + msg.m_filesize = filesize; + pgstat_send(&msg, sizeof(msg)); +} + +; + /* ---------- * pgstat_ping() - * @@ -3185,6 +3208,10 @@ PgstatCollectorMain(int argc, char *argv[]) pgstat_recv_recoveryconflict((PgStat_MsgRecoveryConflict *) &msg, len); break; + case PGSTAT_MTYPE_TEMPFILE: + pgstat_recv_tempfile((PgStat_MsgTempFile *) &msg, len); + break; + default: break; } @@ -3266,6 +3293,8 @@ pgstat_get_db_entry(Oid databaseid, bool create) result->n_conflict_snapshot = 0; result->n_conflict_bufferpin = 0; result->n_conflict_startup_deadlock = 0; + result->n_temp_files = 0; + result->n_temp_bytes = 0; result->stat_reset_timestamp = GetCurrentTimestamp(); @@ -4177,6 +4206,8 @@ pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len) dbentry->n_tuples_updated = 0; dbentry->n_tuples_deleted = 0; dbentry->last_autovac_time = 0; + dbentry->n_temp_bytes = 0; + dbentry->n_temp_files = 0; dbentry->stat_reset_timestamp = GetCurrentTimestamp(); @@ -4403,6 +4434,24 @@ pgstat_recv_recoveryconflict(PgStat_MsgRecoveryConflict *msg, int len) } /* ---------- + * pgstat_recv_tempfile() - + * + * Process as PGSTAT_MTYPE_TEMPFILE message. + * ---------- + */ +static void +pgstat_recv_tempfile(PgStat_MsgTempFile *msg, int len) +{ + PgStat_StatDBEntry *dbentry; + + dbentry = pgstat_get_db_entry(msg->m_databaseid, true); + + dbentry->n_temp_bytes += msg->m_filesize; + dbentry->n_temp_files += 1; + +} + +/* ---------- * pgstat_recv_funcstat() - * * Count what the backend has done. diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 9540279..5e40d12 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -1088,6 +1088,10 @@ FileClose(File file) */ if (vfdP->fdstate & FD_TEMPORARY) { + + struct stat filestats; + int stat_errno; + /* * If we get an error, as could happen within the ereport/elog calls, * we'll come right back here during transaction abort. Reset the @@ -1101,23 +1105,23 @@ FileClose(File file) temporary_files_size -= vfdP->fileSize; vfdP->fileSize = 0; - if (log_temp_files >= 0) - { - struct stat filestats; - int stat_errno; + /* first try the stat() */ + if (stat(vfdP->fileName, &filestats)) + stat_errno = errno; + else + stat_errno = 0; - /* first try the stat() */ - if (stat(vfdP->fileName, &filestats)) - stat_errno = errno; - else - stat_errno = 0; + /* in any case do the unlink */ + if (unlink(vfdP->fileName)) + elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName); - /* in any case do the unlink */ - if (unlink(vfdP->fileName)) - elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName); + /* and last report the stat results */ + if (stat_errno == 0) + { - /* and last report the stat results */ - if (stat_errno == 0) + pgstat_report_tempfile(filestats.st_size); + + if (log_temp_files >= 0) { if ((filestats.st_size / 1024) >= log_temp_files) ereport(LOG, @@ -1131,12 +1135,6 @@ FileClose(File file) elog(LOG, "could not stat file \"%s\": %m", vfdP->fileName); } } - else - { - /* easy case, just do the unlink */ - if (unlink(vfdP->fileName)) - elog(LOG, "could not unlink file \"%s\": %m", vfdP->fileName); - } } /* Unregister it from the resource owner */ diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c index 7792b33..4eeac56 100644 --- a/src/backend/utils/adt/pgstatfuncs.c +++ b/src/backend/utils/adt/pgstatfuncs.c @@ -79,6 +79,8 @@ extern Datum pg_stat_get_db_conflict_bufferpin(PG_FUNCTION_ARGS); extern Datum pg_stat_get_db_conflict_startup_deadlock(PG_FUNCTION_ARGS); extern Datum pg_stat_get_db_conflict_all(PG_FUNCTION_ARGS); extern Datum pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS); +extern Datum pg_stat_get_db_temp_files(PG_FUNCTION_ARGS); +extern Datum pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS); extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS); extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS); @@ -1180,6 +1182,37 @@ pg_stat_get_db_stat_reset_time(PG_FUNCTION_ARGS) } Datum +pg_stat_get_db_temp_files(PG_FUNCTION_ARGS) +{ + Oid dbid = PG_GETARG_OID(0); + int64 result; + PgStat_StatDBEntry *dbentry; + + if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) + result = 0; + else + result = dbentry->n_temp_files; + + PG_RETURN_INT64(result); +} + + +Datum +pg_stat_get_db_temp_bytes(PG_FUNCTION_ARGS) +{ + Oid dbid = PG_GETARG_OID(0); + int64 result; + PgStat_StatDBEntry *dbentry; + + if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL) + result = 0; + else + result = dbentry->n_temp_bytes; + + PG_RETURN_INT64(result); +} + +Datum pg_stat_get_db_conflict_tablespace(PG_FUNCTION_ARGS) { Oid dbid = PG_GETARG_OID(0); diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h index 60e9feb..b4e5feb 100644 --- a/src/include/catalog/pg_proc.h +++ b/src/include/catalog/pg_proc.h @@ -2626,6 +2626,10 @@ DATA(insert OID = 3070 ( pg_stat_get_db_conflict_all PGNSP PGUID 12 1 0 0 0 f f DESCR("statistics: recovery conflicts in database"); DATA(insert OID = 3074 ( pg_stat_get_db_stat_reset_time PGNSP PGUID 12 1 0 0 0 f f f t f s 1 0 1184 "26" _null_ _null_ _null_ _null_ pg_stat_get_db_stat_reset_time _null_ _null_ _null_ )); DESCR("statistics: last reset for a database"); +DATA(insert OID = 3079 ( pg_stat_get_db_temp_files PGNSP PGUID 12 1 0 0 0 f f f t f s 1 0 20 "26" _null_ _null_ _null_ _null_ pg_stat_get_db_temp_files _null_ _null_ _null_ )); +DESCR("statistics: number of temporary files written"); +DATA(insert OID = 3080 ( pg_stat_get_db_temp_bytes PGNSP PGUID 12 1 0 0 0 f f f t f s 1 0 20 "26" _null_ _null_ _null_ _null_ pg_stat_get_db_temp_bytes _null_ _null_ _null_ )); +DESCR("statistics: number of bytes in temporary files written"); DATA(insert OID = 2769 ( pg_stat_get_bgwriter_timed_checkpoints PGNSP PGUID 12 1 0 0 0 f f f t f s 0 0 20 "" _null_ _null_ _null_ _null_ pg_stat_get_bgwriter_timed_checkpoints _null_ _null_ _null_ )); DESCR("statistics: number of timed checkpoints started by the bgwriter"); DATA(insert OID = 2770 ( pg_stat_get_bgwriter_requested_checkpoints PGNSP PGUID 12 1 0 0 0 f f f t f s 0 0 20 "" _null_ _null_ _null_ _null_ pg_stat_get_bgwriter_requested_checkpoints _null_ _null_ _null_ )); diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 651b7d9..de775dd 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -47,7 +47,8 @@ typedef enum StatMsgType PGSTAT_MTYPE_BGWRITER, PGSTAT_MTYPE_FUNCSTAT, PGSTAT_MTYPE_FUNCPURGE, - PGSTAT_MTYPE_RECOVERYCONFLICT + PGSTAT_MTYPE_RECOVERYCONFLICT, + PGSTAT_MTYPE_TEMPFILE } StatMsgType; /* ---------- @@ -377,6 +378,18 @@ typedef struct PgStat_MsgRecoveryConflict } PgStat_MsgRecoveryConflict; /* ---------- + * PgStat_MsgTempFile Sent by the backend upon creating a temp file + * ---------- + */ +typedef struct PgStat_MsgTempFile +{ + PgStat_MsgHdr m_hdr; + + Oid m_databaseid; + size_t m_filesize; +} PgStat_MsgTempFile; + +/* ---------- * PgStat_FunctionCounts The actual per-function counts kept by a backend * * This struct should contain only actual event counters, because we memcmp @@ -507,9 +520,11 @@ typedef struct PgStat_StatDBEntry PgStat_Counter n_conflict_snapshot; PgStat_Counter n_conflict_bufferpin; PgStat_Counter n_conflict_startup_deadlock; + PgStat_Counter n_temp_files; + PgStat_Counter n_temp_bytes; + TimestampTz stat_reset_timestamp; - /* * tables and functions must be last in the struct, because we don't write * the pointers out to the stats file. @@ -716,6 +731,7 @@ extern void pgstat_initialize(void); extern void pgstat_bestart(void); extern void pgstat_report_activity(const char *cmd_str); +extern void pgstat_report_tempfile(size_t filesize); extern void pgstat_report_appname(const char *appname); extern void pgstat_report_xact_timestamp(TimestampTz tstamp); extern void pgstat_report_waiting(bool waiting);