pg_filenode_relation(0,0) elog

Started by Justin Pryzbyalmost 5 years ago4 messageshackers
Jump to latest
#1Justin Pryzby
pryzby@telsasoft.com

Per sqlsmith.

postgres=# SELECT pg_filenode_relation(0,0);
ERROR: unexpected duplicate for tablespace 0, relfilenode 0

postgres=# \errverbose
ERROR: XX000: unexpected duplicate for tablespace 0, relfilenode 0
LOCATION: RelidByRelfilenode, relfilenodemap.c:220

The usual expectation is that sql callable functions should return null rather
than hitting elog(). This also means that sqlsmith has one fewer
false-positive error.

I think it should return NULL if passed invalid relfilenode, rather than
searching pg_class and then writing a pretty scary message about duplicates.

diff --git a/src/backend/utils/cache/relfilenodemap.c b/src/backend/utils/cache/relfilenodemap.c
index 56d7c73d33..5a5cf853bd 100644
--- a/src/backend/utils/cache/relfilenodemap.c
+++ b/src/backend/utils/cache/relfilenodemap.c
@@ -146,6 +146,9 @@ RelidByRelfilenode(Oid reltablespace, Oid relfilenode)
 	ScanKeyData skey[2];
 	Oid			relid;
+	if (!OidIsValid(relfilenode))
+		return InvalidOid;
+
 	if (RelfilenodeMapHash == NULL)
 		InitializeRelfilenodeMap();
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Justin Pryzby (#1)
Re: pg_filenode_relation(0,0) elog

Justin Pryzby <pryzby@telsasoft.com> writes:

Per sqlsmith.
postgres=# SELECT pg_filenode_relation(0,0);
ERROR: unexpected duplicate for tablespace 0, relfilenode 0

Ugh.

The usual expectation is that sql callable functions should return null rather
than hitting elog().

Agreed, but you should put the short-circuit into the SQL-callable
function, ie pg_filenode_relation. Lower-level callers ought not be
passing junk data.

Likely it should check the reltablespace, too.

regards, tom lane

#3Justin Pryzby
pryzby@telsasoft.com
In reply to: Tom Lane (#2)
Re: pg_filenode_relation(0,0) elog

On Fri, Jun 11, 2021 at 11:51:35PM -0400, Tom Lane wrote:

Justin Pryzby <pryzby@telsasoft.com> writes:

Per sqlsmith.
postgres=# SELECT pg_filenode_relation(0,0);
ERROR: unexpected duplicate for tablespace 0, relfilenode 0

Ugh.

The usual expectation is that sql callable functions should return null rather
than hitting elog().

Agreed, but you should put the short-circuit into the SQL-callable
function, ie pg_filenode_relation. Lower-level callers ought not be
passing junk data.

Right. I spent inadequate time reading output of git grep.

Likely it should check the reltablespace, too.

I don't think so. The docs say:
https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBLOCATION
|For a relation in the database's default tablespace, the tablespace can be specified as zero.

Also, that would breaks expected/alter_table.out for the same reason.

diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 3c70bb5943..144aca1099 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -905,6 +905,9 @@ pg_filenode_relation(PG_FUNCTION_ARGS)
 	Oid			relfilenode = PG_GETARG_OID(1);
 	Oid			heaprel = InvalidOid;
+	if (!OidIsValid(relfilenode))
+		PG_RETURN_NULL();
+
 	heaprel = RelidByRelfilenode(reltablespace, relfilenode);

if (!OidIsValid(heaprel))

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Justin Pryzby (#3)
Re: pg_filenode_relation(0,0) elog

Justin Pryzby <pryzby@telsasoft.com> writes:

On Fri, Jun 11, 2021 at 11:51:35PM -0400, Tom Lane wrote:

Likely it should check the reltablespace, too.

I don't think so. The docs say:
https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBLOCATION
|For a relation in the database's default tablespace, the tablespace can be specified as zero.

Right, my mistake. Pushed.

regards, tom lane