On 5/9/2024 18:50, Jelte Fennema-Nio wrote:
On Thu, 5 Sept 2024 at 15:41, Andrei Lepikhov <lepi...@gmail.com> wrote:
It seems that the get_extension_oid routine was not modified when the
sys cache was introduced. What is the reason? It may be that this
routine is redundant now, but if not, and we want to hold the API that
extensions use, maybe we should rewrite it, too.
See the attachment proposing changes.

It seems reasonable to make this function use the new syscache. I
didn't change any existing code in my original patch, because I wanted
to use the syscache APIs directly anyway and I didn't want to make the
patch bigger than strictly necessary. But I totally understand that
for many usages it's probably enough if the existing APIs are simply
faster (on repeated calls). The patch looks fine. But I think
get_extension_name and get_extension_schema should also be updated.
Thanks, see new patch in attachment.

--
regards, Andrei Lepikhov
From 99b8aec9f6f1bb3bc41454016bb855885c8794b0 Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepi...@gmail.com>
Date: Thu, 5 Sep 2024 15:03:10 +0200
Subject: [PATCH] Lookup an extension data in corresponding syscache.

In addition to the commit 490f869, replace search through the pg_extension
relation with much faster lookups in EXTENSIONOID and EXTENSIONNAME syscaches
whenever possible.
---
 src/backend/commands/extension.c | 82 +++++---------------------------
 1 file changed, 13 insertions(+), 69 deletions(-)

diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 1643c8c69a..7e9f764ee0 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -64,6 +64,7 @@
 #include "utils/memutils.h"
 #include "utils/rel.h"
 #include "utils/snapmgr.h"
+#include "utils/syscache.h"
 #include "utils/varlena.h"
 
 
@@ -145,32 +146,9 @@ Oid
 get_extension_oid(const char *extname, bool missing_ok)
 {
        Oid                     result;
-       Relation        rel;
-       SysScanDesc scandesc;
-       HeapTuple       tuple;
-       ScanKeyData entry[1];
-
-       rel = table_open(ExtensionRelationId, AccessShareLock);
-
-       ScanKeyInit(&entry[0],
-                               Anum_pg_extension_extname,
-                               BTEqualStrategyNumber, F_NAMEEQ,
-                               CStringGetDatum(extname));
 
-       scandesc = systable_beginscan(rel, ExtensionNameIndexId, true,
-                                                                 NULL, 1, 
entry);
-
-       tuple = systable_getnext(scandesc);
-
-       /* We assume that there can be at most one matching tuple */
-       if (HeapTupleIsValid(tuple))
-               result = ((Form_pg_extension) GETSTRUCT(tuple))->oid;
-       else
-               result = InvalidOid;
-
-       systable_endscan(scandesc);
-
-       table_close(rel, AccessShareLock);
+       result = GetSysCacheOid1(EXTENSIONNAME, Anum_pg_extension_oid,
+                                                       
CStringGetDatum(extname));
 
        if (!OidIsValid(result) && !missing_ok)
                ereport(ERROR,
@@ -190,32 +168,15 @@ char *
 get_extension_name(Oid ext_oid)
 {
        char       *result;
-       Relation        rel;
-       SysScanDesc scandesc;
        HeapTuple       tuple;
-       ScanKeyData entry[1];
 
-       rel = table_open(ExtensionRelationId, AccessShareLock);
+       tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
 
-       ScanKeyInit(&entry[0],
-                               Anum_pg_extension_oid,
-                               BTEqualStrategyNumber, F_OIDEQ,
-                               ObjectIdGetDatum(ext_oid));
+       if (!HeapTupleIsValid(tuple))
+               return NULL;
 
-       scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
-                                                                 NULL, 1, 
entry);
-
-       tuple = systable_getnext(scandesc);
-
-       /* We assume that there can be at most one matching tuple */
-       if (HeapTupleIsValid(tuple))
-               result = pstrdup(NameStr(((Form_pg_extension) 
GETSTRUCT(tuple))->extname));
-       else
-               result = NULL;
-
-       systable_endscan(scandesc);
-
-       table_close(rel, AccessShareLock);
+       result = pstrdup(NameStr(((Form_pg_extension) 
GETSTRUCT(tuple))->extname));
+       ReleaseSysCache(tuple);
 
        return result;
 }
@@ -229,32 +190,15 @@ Oid
 get_extension_schema(Oid ext_oid)
 {
        Oid                     result;
-       Relation        rel;
-       SysScanDesc scandesc;
        HeapTuple       tuple;
-       ScanKeyData entry[1];
-
-       rel = table_open(ExtensionRelationId, AccessShareLock);
 
-       ScanKeyInit(&entry[0],
-                               Anum_pg_extension_oid,
-                               BTEqualStrategyNumber, F_OIDEQ,
-                               ObjectIdGetDatum(ext_oid));
+       tuple = SearchSysCache1(EXTENSIONOID, ObjectIdGetDatum(ext_oid));
 
-       scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
-                                                                 NULL, 1, 
entry);
-
-       tuple = systable_getnext(scandesc);
-
-       /* We assume that there can be at most one matching tuple */
-       if (HeapTupleIsValid(tuple))
-               result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
-       else
-               result = InvalidOid;
-
-       systable_endscan(scandesc);
+       if (!HeapTupleIsValid(tuple))
+               return InvalidOid;
 
-       table_close(rel, AccessShareLock);
+       result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
+       ReleaseSysCache(tuple);
 
        return result;
 }
-- 
2.46.0

Reply via email to