Обсуждение: Getting my Database name in a C Extension
Hi, I'm writing an extention (FDW), and I need, in my C code, the name of my database ("contrib_regression_test" for example), and I've two questions: 1) What is the easiest way to get that directly in C? 2) Is there a way to get this information in the SQL extension installation/update scripts (like the @/extschema/@ substitution)? Thanks, Cedric
Cedric Berger <cedric@precidata.com> writes: > I'm writing an extention (FDW), and I need, in my C code, the name of > my database ("contrib_regression_test" for example), and I've two > questions: > 1) What is the easiest way to get that directly in C? The usual locution is "get_database_name(MyDatabaseId)". > 2) Is there a way to get this information in the SQL extension > installation/update scripts (like the @/extschema/@ substitution)? Nope. Doesn't seem to me like a remarkably good idea to refer to it in that sort of way anyway. What would happen if someone renamed the database after the extension is installed? regards, tom lane
On 02/10/14 18:13, Tom Lane wrote: > Cedric Berger <cedric@precidata.com> writes: > >> 1) What is the easiest way to get that directly in C? > > The usual locution is "get_database_name(MyDatabaseId)". Ok, but then how do I find "MyDatabaseId" in, say, a BeginForeignScan() or GetForeignRelSize() FDW callback? http://www.postgresql.org/docs/9.3/static/fdw-callbacks.html Sorry for these beginner's questions, but I've trouble navigating the documentation / source / examples for this kind of info. PS: is "which see for additional details" really good English in the fdw-callbacks.html documentation? >> 2) Is there a way to get this information in the SQL extension >> installation/update scripts (like the @/extschema/@ substitution)? > > Nope. Doesn't seem to me like a remarkably good idea to refer to it > in that sort of way anyway. What would happen if someone renamed > the database after the extension is installed? Ok, Make sense, Thanks Cedric
Cedric Berger wrote: >>> 1) What is the easiest way to get that directly in C? >> >> The usual locution is "get_database_name(MyDatabaseId)". > > Ok, but then how do I find "MyDatabaseId" in, say, > a BeginForeignScan() or GetForeignRelSize() FDW callback? It is a global, all you should have to do is #include "miscadmin.h" Yours, Laurenz Albe
On Fri, Oct 3, 2014 at 4:57 PM, Albe Laurenz <laurenz.albe@wien.gv.at> wrote:
> Cedric Berger wrote:
>>>> 1) What is the easiest way to get that directly in C?
>>>
>>> The usual locution is "get_database_name(MyDatabaseId)".
>>
>> Ok, but then how do I find "MyDatabaseId" in, say,
>> a BeginForeignScan() or GetForeignRelSize() FDW callback?
>
> It is a global, all you should have to do is
> #include "miscadmin.h"
When looking for a global variable, a command like that is generally useful:
$ git grep MyDatabaseId -- *.h
src/include/access/xact.h: Oid dbId; /* MyDatabaseId */
src/include/miscadmin.h:extern PGDLLIMPORT Oid MyDatabaseId;
--
Michael
Michael
On 03/10/14 10:23, Michael Paquier wrote: > On Fri, Oct 3, 2014 at 4:57 PM, Albe Laurenz <laurenz.albe@wien.gv.at > <mailto:laurenz.albe@wien.gv.at>> wrote: > > Cedric Berger wrote: > >>>> 1) What is the easiest way to get that directly in C? > >>> > >>> The usual locution is "get_database_name(MyDatabaseId)". > >> > >> Ok, but then how do I find "MyDatabaseId" in, say, > >> a BeginForeignScan() or GetForeignRelSize() FDW callback? > > > > It is a global, all you should have to do is > > #include "miscadmin.h" > When looking for a global variable, a command like that is generally useful: > $ git grep MyDatabaseId -- *.h > src/include/access/xact.h: Oid dbId; > /* MyDatabaseId */ > src/include/miscadmin.h:extern PGDLLIMPORT Oid MyDatabaseId; Thanks a lot! I didn't think 'MyDatabaseId' was a real variable name... Cedric