Discussion:
Getting my Database name in a C Extension
Cedric Berger
2014-10-02 15:38:27 UTC
Permalink
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
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Tom Lane
2014-10-02 16:13:33 UTC
Permalink
Post by Cedric Berger
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
1) What is the easiest way to get that directly in C?
The usual locution is "get_database_name(MyDatabaseId)".
Post by Cedric Berger
2) Is there a way to get this information in the SQL extension
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
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Cedric Berger
2014-10-03 07:31:54 UTC
Permalink
Post by Tom Lane
Post by Cedric Berger
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?
Post by Tom Lane
Post by Cedric Berger
2) Is there a way to get this information in the SQL extension
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
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Albe Laurenz
2014-10-03 07:57:08 UTC
Permalink
Post by Cedric Berger
Post by Tom Lane
Post by Cedric Berger
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
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.or
Michael Paquier
2014-10-03 08:23:37 UTC
Permalink
Post by Albe Laurenz
Post by Cedric Berger
Post by Tom Lane
Post by Cedric Berger
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;
Regards,
--
Michael
Cedric Berger
2014-10-03 11:54:44 UTC
Permalink
Post by Michael Paquier
Post by Albe Laurenz
Post by Cedric Berger
Post by Tom Lane
Post by Cedric Berger
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"
$ 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
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Cedric Berger
2014-10-02 15:11:32 UTC
Permalink
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 Berger
Precidata Sarl
Maladière 71c
2000 Neuchâtel
***@precidata.com
032 930 29 62
079 934 11 02
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
John R Pierce
2014-10-02 23:01:03 UTC
Permalink
Post by Cedric Berger
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
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
you could SELECT CURRENT_CATALOG; using the SPI_ functions from your C,
or directly in your SQL extension script... this returns the database
name (the name is from the sql standard). see
http://www.postgresql.org/docs/current/static/functions-info.html for
more info functions like this available in SQL.
--
john r pierce 37N 122W
somewhere on the middle of the left coast
Loading...