From 57dfc114d0fd64b3a87c2bcd553fd5511e1f5769 Mon Sep 17 00:00:00 2001 From: Surya Poondla Date: Tue, 3 Nov 2026 15:17:11 -0800 Subject: [PATCH v3] pg_freespacemap: Reject temp/unlogged relations it cannot read pg_freespace() silently returned 0 for an unlogged relation on a standby, as its FSM fork does not exist there, while the primary reports the real value. For a temporary table of another session, it returned 0 if the table had no FSM fork yet, but failed with "cannot access temporary tables of other sessions" once it had one. Report an error in both cases, matching the checks added to pg_visibility and the other contrib modules that read relation pages. --- contrib/pg_freespacemap/pg_freespacemap.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/contrib/pg_freespacemap/pg_freespacemap.c b/contrib/pg_freespacemap/pg_freespacemap.c index 610fc90b5ba..4952468695d 100644 --- a/contrib/pg_freespacemap/pg_freespacemap.c +++ b/contrib/pg_freespacemap/pg_freespacemap.c @@ -9,6 +9,7 @@ #include "postgres.h" #include "access/relation.h" +#include "access/xlog.h" #include "fmgr.h" #include "storage/freespace.h" #include "utils/rel.h" @@ -41,6 +42,22 @@ pg_freespace(PG_FUNCTION_ARGS) RelationGetRelationName(rel)), errdetail_relkind_not_supported(rel->rd_rel->relkind))); + /* + * Reject attempts to read non-local temporary relations; we would be + * likely to get wrong data since we have no visibility into the owning + * session's local buffers. + */ + if (RELATION_IS_OTHER_TEMP(rel)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot access temporary tables of other sessions"))); + + /* Temporary and unlogged relations are inaccessible during recovery. */ + if (!RelationIsPermanent(rel) && RecoveryInProgress()) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot access temporary or unlogged relations during recovery"))); + if (blkno < 0 || blkno > MaxBlockNumber) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), -- 2.39.5 (Apple Git-154)