From 9b8e1eb375e6d8b8568a3441bdc316341fd7b640 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 24 Mar 2008 18:22:36 +0000 Subject: [PATCH] Adjust the recent patch for reporting of deadlocked queries so that we report query texts only to the server log. This eliminates the issue of possible leaking of security-sensitive data in other sessions' queries. Since the log is presumed secure, we can now log the queries of all sessions involved in the deadlock, whether or not they belong to the same user as the one reporting the failure. --- src/backend/postmaster/pgstat.c | 6 ++--- src/backend/storage/lmgr/deadlock.c | 36 ++++++++++++++++++----------- src/include/pgstat.h | 4 ++-- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index 8259b759e7..642943c821 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -13,7 +13,7 @@ * * Copyright (c) 2001-2008, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.170 2008/03/21 21:08:31 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.171 2008/03/24 18:22:36 tgl Exp $ * ---------- */ #include "postgres.h" @@ -2056,7 +2056,7 @@ pgstat_read_current_status(void) * ---------- */ const char * -pgstat_get_backend_current_activity(int pid) +pgstat_get_backend_current_activity(int pid, bool checkUser) { PgBackendStatus *beentry; int i; @@ -2094,7 +2094,7 @@ pgstat_get_backend_current_activity(int pid) if (found) { /* Now it is safe to use the non-volatile pointer */ - if (!superuser() && beentry->st_userid != GetUserId()) + if (checkUser && !superuser() && beentry->st_userid != GetUserId()) return ""; else if (*(beentry->st_activity) == '\0') return ""; diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c index e1a66456f5..97be7d9dc7 100644 --- a/src/backend/storage/lmgr/deadlock.c +++ b/src/backend/storage/lmgr/deadlock.c @@ -12,7 +12,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.52 2008/03/21 21:08:31 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/storage/lmgr/deadlock.c,v 1.53 2008/03/24 18:22:36 tgl Exp $ * * Interface: * @@ -879,15 +879,16 @@ PrintLockQueue(LOCK *lock, const char *info) void DeadLockReport(void) { - StringInfoData detailbuf; - StringInfoData contextbuf; + StringInfoData clientbuf; /* errdetail for client */ + StringInfoData logbuf; /* errdetail for server log */ StringInfoData locktagbuf; int i; - initStringInfo(&detailbuf); - initStringInfo(&contextbuf); + initStringInfo(&clientbuf); + initStringInfo(&logbuf); initStringInfo(&locktagbuf); + /* Generate the "waits for" lines sent to the client */ for (i = 0; i < nDeadlockDetails; i++) { DEADLOCK_INFO *info = &deadlockDetails[i]; @@ -905,30 +906,39 @@ DeadLockReport(void) DescribeLockTag(&locktagbuf, &info->locktag); if (i > 0) - appendStringInfoChar(&detailbuf, '\n'); + appendStringInfoChar(&clientbuf, '\n'); - appendStringInfo(&detailbuf, + appendStringInfo(&clientbuf, _("Process %d waits for %s on %s; blocked by process %d."), info->pid, GetLockmodeName(info->locktag.locktag_lockmethodid, info->lockmode), locktagbuf.data, nextpid); + } - if (i > 0) - appendStringInfoChar(&contextbuf, '\n'); + /* Duplicate all the above for the server ... */ + appendStringInfoString(&logbuf, clientbuf.data); + + /* ... and add info about query strings */ + for (i = 0; i < nDeadlockDetails; i++) + { + DEADLOCK_INFO *info = &deadlockDetails[i]; + + appendStringInfoChar(&logbuf, '\n'); - appendStringInfo(&contextbuf, + appendStringInfo(&logbuf, _("Process %d: %s"), info->pid, - pgstat_get_backend_current_activity(info->pid)); + pgstat_get_backend_current_activity(info->pid, false)); } ereport(ERROR, (errcode(ERRCODE_T_R_DEADLOCK_DETECTED), errmsg("deadlock detected"), - errdetail("%s", detailbuf.data), - errcontext("%s", contextbuf.data))); + errdetail("%s", clientbuf.data), + errdetail_log("%s", logbuf.data), + errhint("See server log for query details."))); } /* diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 49fd149d55..fdc846acad 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -5,7 +5,7 @@ * * Copyright (c) 2001-2008, PostgreSQL Global Development Group * - * $PostgreSQL: pgsql/src/include/pgstat.h,v 1.72 2008/03/21 21:08:31 tgl Exp $ + * $PostgreSQL: pgsql/src/include/pgstat.h,v 1.73 2008/03/24 18:22:36 tgl Exp $ * ---------- */ #ifndef PGSTAT_H @@ -507,7 +507,7 @@ extern void pgstat_bestart(void); extern void pgstat_report_activity(const char *what); extern void pgstat_report_xact_timestamp(TimestampTz tstamp); extern void pgstat_report_waiting(bool waiting); -extern const char *pgstat_get_backend_current_activity(int pid); +extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser); extern void pgstat_initstats(Relation rel); -- 2.50.1