Subject: Obsolete reference to pg_relation in comment

From: Dagfinn Ilmari Mannsåker <[email-masked]>
Date: Wed, 26 Jul 2023 18:48:51 +0100

Hi hackers,

Triggered by a discussion on IRC, I noticed that there's a stray
reference to pg_relation in a comment that was added long after it was
renamed to pg_class.  Here's a patch to bring that up to speed.

- ilmari

>From e395f8cb293f674f45eb3847534de07c7124e738 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= <[email-masked]>
Date: Wed, 26 Jul 2023 18:31:51 +0100
Subject: [PATCH] Fix obsolete reference to pg_relation in comment

pg_relation was renamed to pg_class in 1991, but this comment (added
in 2004) missed the memo
---
 src/backend/storage/large_object/inv_api.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/storage/large_object/inv_api.c b/src/backend/storage/large_object/inv_api.c
index 84e543e731..a56912700b 100644
--- a/src/backend/storage/large_object/inv_api.c
+++ b/src/backend/storage/large_object/inv_api.c
@@ -59,7 +59,7 @@ bool		lo_compat_privileges;
 
 /*
  * All accesses to pg_largeobject and its index make use of a single Relation
- * reference, so that we only need to open pg_relation once per transaction.
+ * reference, so that we only need to open pg_class once per transaction.
  * To avoid problems when the first such reference occurs inside a
  * subtransaction, we execute a slightly klugy maneuver to assign ownership of
  * the Relation reference to TopTransactionResourceOwner.
-- 
2.39.2

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 26 Jul 2023 11:53:06 -0700

> pg_relation was renamed to pg_class in 1991, but this comment (added

Huh, interesting!  I dug around the Berkeley archives [0] and found
comments indicating that pg_relation was renamed to pg_class in Februrary
1990.  However, it looks like the file was named pg_relation.h until
Postgres95 v0.01, which has the following comment in pg_class.h:

	 *    ``pg_relation'' is being replaced by ``pg_class''.  currently
	 *    we are only changing the name in the catalogs but someday the
	 *    code will be changed too. -cim 2/26/90
	 *    [it finally happens.  -ay 11/5/94]

[0] https://dsf.berkeley.edu/postgres.html

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 26 Jul 2023 12:11:53 -0700

> 	 *    [it finally happens.  -ay 11/5/94]

This comment actually lived in Postgres until 9cf80f2 (June 2000), too.

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 26 Jul 2023 13:50:31 -0700

Okay, now looking at the patch...

>   * the Relation reference to TopTransactionResourceOwner.

Hm.  Are you sure this is actually referring to pg_class?  It seems
unlikely given pg_relation was renamed 14 years before this comment was
added, and the code appears to be ensuring that pg_largeobject and its
index are opened at most once per transaction.  I couldn't find the
original thread for this comment, unfortunately, but ISTM we might want to
replace "pg_relation" with "them" instead.

---

From: Tom Lane <[email-masked]>
Date: Wed, 26 Jul 2023 17:14:08 -0400

> index are opened at most once per transaction.

I believe it is just a typo/thinko for pg_class, but there's more not
to like about this comment.  First, once we've made a relcache entry
it would typically stay valid across uses, so it's far from clear that
this coding actually prevents many catalog accesses in typical cases.
Second, when we do have to rebuild the relcache entry, there's a lot
more involved than just a pg_class fetch; we at least need to read
pg_attribute, and I think there may be other catalogs that we'd read
along the way, even for a system catalog that lacks complicated
features.  (pg_index would presumably get looked at, for instance.)

I think we should reword this to just generically claim that holding
the Relation reference open for the whole transaction reduces overhead.

			regards, tom lane

---

From: Nathan Bossart <[email-masked]>
Date: Wed, 26 Jul 2023 14:20:36 -0700

> the Relation reference open for the whole transaction reduces overhead.

WFM

---

From: Bruce Momjian <[email-masked]>
Date: Wed, 6 Sep 2023 15:13:20 -0400

> the Relation reference open for the whole transaction reduces overhead.

How is this attached patch?

-- 
  Bruce Momjian  <[email-masked]>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Only you can decide what is important to you.
diff --git a/src/backend/storage/large_object/inv_api.c b/src/backend/storage/large_object/inv_api.c
new file mode 100644
index 84e543e..cc9c335
*** a/src/backend/storage/large_object/inv_api.c
--- b/src/backend/storage/large_object/inv_api.c
***************
*** 58,68 ****
  bool		lo_compat_privileges;
  
  /*
!  * All accesses to pg_largeobject and its index make use of a single Relation
!  * reference, so that we only need to open pg_relation once per transaction.
!  * To avoid problems when the first such reference occurs inside a
!  * subtransaction, we execute a slightly klugy maneuver to assign ownership of
!  * the Relation reference to TopTransactionResourceOwner.
   */
  static Relation lo_heap_r = NULL;
  static Relation lo_index_r = NULL;
--- 58,68 ----
  bool		lo_compat_privileges;
  
  /*
!  * All accesses to pg_largeobject and its index make use of a single
!  * Relation reference.  To guarantee that the relcache entry remains
!  * in the cache, on the first reference inside a subtransaction, we
!  * execute a slightly klugy maneuver to assign ownership of the
!  * Relation reference to TopTransactionResourceOwner.
   */
  static Relation lo_heap_r = NULL;
  static Relation lo_index_r = NULL;

---

From: Daniel Gustafsson <[email-masked]>
Date: Thu, 7 Sep 2023 10:44:25 +0200

> On 6 Sep 2023, at 21:13, Bruce Momjian <[email-masked]> wrote:

> How is this attached patch?

Reads good to me, +1.

--
Daniel Gustafsson

---

From: Bruce Momjian <[email-masked]>
Date: Tue, 26 Sep 2023 17:07:45 -0400

> > How is this attached patch?

Patch applied to master.  I didn't think backpatching it made much sense
since it is so localized.