On Fri, Mar 13, 2026 at 11:59 AM Corey Huinker <[email protected]> wrote:
>
> Can you explain this bit below?
>
> @@ -595,13 +605,23 @@ RangeVarGetRelidExtended(const RangeVar *relation, 
> LOCKMODE lockmode,
>   {
>   int elevel = (flags & RVR_SKIP_LOCKED) ? DEBUG1 : ERROR;
>
> - if (relation->schemaname)
> - ereport(elevel,
> + if (relation->schemaname && elevel == DEBUG1)
> + ereport(DEBUG1,
>   (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
>   errmsg("could not obtain lock on relation \"%s.%s\"",
>   relation->schemaname, relation->relname)));
> - else
> - ereport(elevel,
> + else if (relation->schemaname && elevel == ERROR)
> + ereturn(escontext, InvalidOid,
> + errcode(ERRCODE_LOCK_NOT_AVAILABLE),
> + errmsg("could not obtain lock on relation \"%s.%s\"",
> +   relation->schemaname, relation->relname));
> + else if (elevel == DEBUG1)
> + ereport(DEBUG1,
> + errcode(ERRCODE_LOCK_NOT_AVAILABLE),
> + errmsg("could not obtain lock on relation \"%s\"",
> +   relation->relname));
> + else if (elevel == ERROR)
> + ereturn(escontext, InvalidOid,
>

This is for supporting casting text to regclass error safe, for example:
SELECT CAST('abc'::text as regclass default NULL on conversion error);

To do that, we need to refactor the RangeVarGetRelidExtended function
to make it error-safe.
We can also change it as:

if (relation->schemaname)
{
    if (elevel == DEBUG1)
        ereport(DEBUG1,
                (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                errmsg("could not obtain lock on relation \"%s.%s\"",
                        relation->schemaname, relation->relname)));
    else
        ereturn(escontext, InvalidOid,
                errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                errmsg("could not obtain lock on relation \"%s.%s\"",
                    relation->schemaname, relation->relname));
}
else
{
    if (elevel == DEBUG1)
        ereport(DEBUG1,
                errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                errmsg("could not obtain lock on relation \"%s\"",
                    relation->relname));
    else
        ereturn(escontext, InvalidOid,
                (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                errmsg("could not obtain lock on relation \"%s\"",
                        relation->relname)));
}


Reply via email to