The following commit added relkind checks to certain contrib modules so
that a more user-friendly error is produced if the wrong kind of relation
is passed to its functions:

commit c08d82f38ebf763b79bd43ae34b7310ee47aaacd
Author: Stephen Frost <sfr...@snowman.net>
Date:   Thu Mar 9 16:34:25 2017 -0500

    Add relkind checks to certain contrib modules

But it missed pgrowlocks, so the following happens:

create extension pgrowlocks;
create view one as select 1;
select pgrowlocks('one');
-- ERROR:  could not open file "base/68730/68748": No such file or directory

With the attached patch:

select pgrowlocks('one');
ERROR:  "one" is not a table, index, materialized view, sequence, or TOAST
table

Thanks,
Amit
>From c9cd61e7c78f3d8f8744a807399b22172055597b Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Tue, 11 Apr 2017 16:59:03 +0900
Subject: [PATCH] Add relkind check to pgrowlocks

---
 contrib/pgrowlocks/pgrowlocks.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/contrib/pgrowlocks/pgrowlocks.c b/contrib/pgrowlocks/pgrowlocks.c
index 8dd561c02a..900b034f1e 100644
--- a/contrib/pgrowlocks/pgrowlocks.c
+++ b/contrib/pgrowlocks/pgrowlocks.c
@@ -66,6 +66,8 @@ typedef struct
 #define		Atnum_modes		4
 #define		Atnum_pids		5
 
+static void check_relation_relkind(Relation rel);
+
 Datum
 pgrowlocks(PG_FUNCTION_ARGS)
 {
@@ -109,6 +111,9 @@ pgrowlocks(PG_FUNCTION_ARGS)
 			aclcheck_error(aclresult, ACL_KIND_CLASS,
 						   RelationGetRelationName(rel));
 
+		/* Only some relkinds contain rows */
+		check_relation_relkind(rel);
+
 		scan = heap_beginscan(rel, GetActiveSnapshot(), 0, NULL);
 		mydata = palloc(sizeof(*mydata));
 		mydata->rel = rel;
@@ -295,3 +300,21 @@ pgrowlocks(PG_FUNCTION_ARGS)
 
 	SRF_RETURN_DONE(funcctx);
 }
+
+/*
+ * check_relation_relkind - convenience routine to check that relation
+ * is of the relkind supported by the callers
+ */
+static void
+check_relation_relkind(Relation rel)
+{
+	if (rel->rd_rel->relkind != RELKIND_RELATION &&
+		rel->rd_rel->relkind != RELKIND_INDEX &&
+		rel->rd_rel->relkind != RELKIND_MATVIEW &&
+		rel->rd_rel->relkind != RELKIND_SEQUENCE &&
+		rel->rd_rel->relkind != RELKIND_TOASTVALUE)
+		ereport(ERROR,
+				(errcode(ERRCODE_WRONG_OBJECT_TYPE),
+				 errmsg("\"%s\" is not a table, index, materialized view, sequence, or TOAST table",
+						RelationGetRelationName(rel))));
+}
-- 
2.11.0

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to