postgres.git / summary / log / commit / refs
commit 0091c241f6cffb2b163f18dea54c10d0b45090aa
Author: Michael Paquier <michael@paquier.xyz>
Date: Mon Nov 09 08:03:59 2026 +0000
pgstattuple: Reject unlogged and temporary relations during recovery
Unlogged and temporary relations have no data on a standby, and trying
to query these would lead to the following failures:
ERROR: could not open file "base/MMM/NNN": No such file or directory
These cases are now blocked with a much better error message. Temporary
relations from other sessions were already blocked. pg_relpages() is an
exception and can work for temp tables created by other sessions.
While on it, the error messages are clarified to depend on the relkind.
Some of the functions reported relkinds unrelated to the relation
queried.
Reported-by: Zsolt Parragi <zsolt.parragi@percona.com>
Author: Jim Jones <jim.jones@uni-muenster.de>
Author: Kirill Reshke <reshkekirill@gmail.com>
Author: Surya Poondla <suryapoondla4@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/82ba0876-6127-4770-b900-aa1bd52abfed@uni-muenster.de
contrib/pgstattuple/pgstatapprox.c | 11 ++---------
contrib/pgstattuple/pgstatindex.c | 36 +++++++++---------------------------
contrib/pgstattuple/pgstattuple.c | 37 +++++++++++++++++++++++++++++++------
contrib/pgstattuple/pgstattuple.h | 21 +++++++++++++++++++++
4 files changed, 63 insertions(+), 42 deletions(-)
diff --git a/contrib/pgstattuple/pgstatapprox.c b/contrib/pgstattuple/pgstatapprox.c
index 21e0b50fb4b..ade1b3bcebd 100644
--- a/contrib/pgstattuple/pgstatapprox.c
+++ b/contrib/pgstattuple/pgstatapprox.c
@@ -20,6 +20,7 @@
#include "commands/vacuum.h"
#include "funcapi.h"
#include "miscadmin.h"
+#include "pgstattuple.h"
#include "storage/bufmgr.h"
#include "storage/freespace.h"
#include "storage/procarray.h"
@@ -322,15 +323,7 @@ pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo)
rel = relation_open(relid, AccessShareLock);
- /*
- * 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")));
+ check_relation_supported(rel);
/*
* We support only relation kinds with a visibility map and a free space
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index 8951ad0aac4..f79f5e2a65e 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -32,10 +32,12 @@
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "access/relation.h"
+#include "access/xlog.h"
#include "catalog/namespace.h"
#include "catalog/pg_am.h"
#include "funcapi.h"
#include "miscadmin.h"
+#include "pgstattuple.h"
#include "storage/bufmgr.h"
#include "storage/read_stream.h"
#include "utils/rel.h"
@@ -228,15 +230,7 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
errmsg("relation \"%s\" is not a btree index",
RelationGetRelationName(rel))));
- /*
- * 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")));
+ check_relation_supported(rel);
/*
* A !indisready index could lead to ERRCODE_DATA_CORRUPTED later, so exit
@@ -483,6 +477,10 @@ pg_relpages_impl(Relation rel)
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
/* note: this will work OK on non-local temp tables */
+ if (!RelationIsPermanent(rel) && RecoveryInProgress())
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot access temporary or unlogged relations during recovery")));
relpages = RelationGetNumberOfBlocks(rel);
@@ -547,15 +545,7 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
errmsg("relation \"%s\" is not a GIN index",
RelationGetRelationName(rel))));
- /*
- * 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 indexes of other sessions")));
+ check_relation_supported(rel);
/* see pgstatindex_impl */
if (!rel->rd_index->indisvalid)
@@ -637,15 +627,7 @@ pgstathashindex(PG_FUNCTION_ARGS)
errmsg("relation \"%s\" is not a hash index",
RelationGetRelationName(rel))));
- /*
- * 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 indexes of other sessions")));
+ check_relation_supported(rel);
/* see pgstatindex_impl */
if (!rel->rd_index->indisvalid)
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index 6a7f8cb4a7c..d3e21f52e4a 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -30,10 +30,12 @@
#include "access/nbtree.h"
#include "access/relscan.h"
#include "access/tableam.h"
+#include "access/xlog.h"
#include "catalog/namespace.h"
#include "catalog/pg_am_d.h"
#include "funcapi.h"
#include "miscadmin.h"
+#include "pgstattuple.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/varlena.h"
@@ -237,22 +239,45 @@ pgstattuplebyid_v1_5(PG_FUNCTION_ARGS)
}
/*
- * pgstat_relation
+ * check_relation_supported
+ *
+ * Check that a relation can be read by this session.
*/
-static Datum
-pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
+void
+check_relation_supported(Relation rel)
{
- const char *err;
-
/*
* 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))
+ {
+ if (rel->rd_rel->relkind == RELKIND_INDEX)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot access temporary indexes of other sessions")));
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot access temporary tables of other sessions")));
+ }
+
+ if (!RelationIsPermanent(rel) && RecoveryInProgress())
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot access temporary tables of other sessions")));
+ errmsg("cannot access temporary or unlogged relations during recovery")));
+}
+
+/*
+ * pgstat_relation
+ */
+static Datum
+pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
+{
+ const char *err;
+
+ check_relation_supported(rel);
if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) ||
rel->rd_rel->relkind == RELKIND_SEQUENCE)
diff --git a/contrib/pgstattuple/pgstattuple.h b/contrib/pgstattuple/pgstattuple.h
new file mode 100644
index 00000000000..b66e41a9609
--- /dev/null
+++ b/contrib/pgstattuple/pgstattuple.h
@@ -0,0 +1,21 @@
+/*-------------------------------------------------------------------------
+ *
+ * pgstattuple.h
+ * Common functions for pgstattuple.
+ *
+ * Copyright (c) 2026, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/pgstattuple/pgstattuple.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef _PGSTATTUPLE_H_
+#define _PGSTATTUPLE_H_
+
+#include "utils/relcache.h"
+
+/* in pgstattuple.c */
+extern void check_relation_supported(Relation rel);
+
+#endif /* _PGSTATTUPLE_H_ */
[parent: 553262aff40d]