try_relation_open and relation_open behave different. - Mailing list pgsql-hackers

From Xing GUO
Subject try_relation_open and relation_open behave different.
Date
Msg-id CACpMh+C00FnGkW=qPX7UQxSWh0Xhz-Nx5tJr+SpMjcmsUXDnJQ@mail.gmail.com
Whole thread Raw
Responses Re: try_relation_open and relation_open behave different.  (Michael Paquier <michael@paquier.xyz>)
List pgsql-hackers
Hi hackers,

I'm writing an extension that employs `object_access_hook`. I want to monitor the table creation event and record the mapping between `reloid` and `relfilenode` during a transaction. Here's my code snippet,

```
static void
my_object_access_hook(ObjectAccessType access,
                      Oid classId,
                      Oid objectId,
                      int subId, void *arg)
{
    do_some_checks(access, classId, ...);
    // open the relation using relation_open
    rel = relation_open(objectId, AccessShareLock);

    // record the reloid and relfilenode.
    record(objectId, rel->rd_node);
    relation_close(rel, AccessShareLock);
}
```

However, when I replace the relation_open with try_relation_open, the relation cannot be opened. I've checked the source code, it looks that try_relation_open has an additional checker which causes the relation_open and try_relation_open behavior different:

```
Relation
try_relation_open(Oid relationId, LOCKMODE lockmode)
{
    ...
    /*
     * Now that we have the lock, probe to see if the relation really exists
     * or not.
     */
    if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relationId)))
    {
        /* Release useless lock */
        if (lockmode != NoLock)
           UnlockRelationOid(relationId, lockmode);

        return NULL;
    }
    ...
}
```


My question is, is it a deliberate design that makes try_relation_open and relation_open different? Shall we mention it in the comment of try_relation_open OR adding the checker to relation_open?

Best Regards,
Xing






pgsql-hackers by date:

Previous
From: Bharath Rupireddy
Date:
Subject: Re: can we add subscription TAP test option "vcregress subscriptioncheck" for MSVC builds?
Next
From: Amit Kapila
Date:
Subject: Re: Data is copied twice when specifying both child and parent table in publication