pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.63k stars 248 forks source link

Missing API for parsing qualified name string #164

Closed Horusiath closed 9 months ago

Horusiath commented 3 years ago

I've tried to build a custom extension with a function that could take a name of an index or column as a parameter and do some work on it. After some time I realized, that I need to somehow obtain an oid for that object in order to make further work.

Using C API this looks more or less like this:

#include "postgres.h"
#include "utils/repgroc.h"
#include "catalog/namespace.h"

static Oid GetOid() {
    text* name = PG__GETARG_TEXT_P(0);
    char* relname = t2c(name);
    List* relname_list = stringToQualifiedNameList(relname, "");
    RangeVar* relvar = makeRangeVarFromNameList(relname_list);
    Oid relOid = RangeVarGetRelid(relvar, AccessExclusiveLock, false);
    return relOid;
}

static char* t2c(text* in) {
    char* out = palloc(VARSIZE(in));
    memcpy(out, VARDATA(in), VARSIZE(in)-VARHDRSZ);
    out[VARSIZE(in) - VARHDRSZ] = '\0';
    return out;
}

The thing is that utils/regproc.h file is not referenced by pgx, and therefore not all of the necessary functions - like stringToQualifiedNameList - are exposed.

timClicks commented 3 years ago

Thank you for raising this issue @Horusiath. We will look into exposing the regproc.h API.

This has also being discussed within our Discord channel https://discord.com/channels/561648697805504526/749454413407584258/857511256529043456