v6-0004-ensure-pg_stats_vacuum-object-is-either-relati.no-cfbot

application/octet-stream

Filename: v6-0004-ensure-pg_stats_vacuum-object-is-either-relati.no-cfbot
Type: application/octet-stream
Part: 2
Message: Re: Vacuum statistics
From d55543911dc31b83b4e210f9015624e71dc2aca8 Mon Sep 17 00:00:00 2001
From: jian he <jian.universality@gmail.com>
Date: Thu, 22 Aug 2024 09:39:02 +0800
Subject: [PATCH v6 4/4] ensure pg_stats_vacuum object is either relation or
 index

---
 src/backend/utils/adt/pgstatfuncs.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index 458096950e..1908eaf3af 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -30,6 +30,7 @@
 #include "storage/procarray.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
+#include "utils/lsyscache.h"
 #include "utils/timestamp.h"
 #include "utils/pgstat_internal.h"
 
@@ -2191,6 +2192,7 @@ pg_stats_vacuum(FunctionCallInfo fcinfo, ExtVacReportType type, int ncolumns)
 	ReturnSetInfo		   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
 	Oid						dbid = PG_GETARG_OID(0);
 	PgStat_StatTabEntry    *tabentry;
+	char					relkind;
 
 	InitMaterializedSRF(fcinfo, 0);
 
@@ -2210,6 +2212,12 @@ pg_stats_vacuum(FunctionCallInfo fcinfo, ExtVacReportType type, int ncolumns)
 		/* Load table statistics for specified database. */
 		if (OidIsValid(relid))
 		{
+			relkind =  get_rel_relkind(relid);
+			if (relkind != RELKIND_RELATION && relkind != RELKIND_INDEX)
+				ereport(ERROR,
+						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+						 errmsg("object \"%d\" is not a table or index", relid)));
+
 			tabentry = fetch_dbstat_tabentry(dbid, relid);
 			if (tabentry == NULL || tabentry->vacuum_ext.type != type)
 				/* Table don't exists or isn't an heap relation. */
@@ -2234,6 +2242,13 @@ pg_stats_vacuum(FunctionCallInfo fcinfo, ExtVacReportType type, int ncolumns)
 			{
 				Oid	reloid;
 
+				//TODO do we need do this?
+				relkind =  get_rel_relkind(relid);
+				if (relkind != RELKIND_RELATION && relkind != RELKIND_INDEX)
+					ereport(ERROR,
+							(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+							 errmsg("object \"%d\" is not a table or index", relid)));
+
 				CHECK_FOR_INTERRUPTS();
 
 				tabentry = (PgStat_StatTabEntry *) entry->data;
-- 
2.34.1