</para>
<para>Once running, the process can connect to a database by calling
- <function>BackgroundWorkerInitializeConnection(<parameter>char *dbname</parameter>, <parameter>char *username</parameter>)</function>.
+ <function>BackgroundWorkerInitializeConnection(<parameter>char *dbname</parameter>, <parameter>char *username</parameter>)</function> or
+ <function>BackgroundWorkerInitializeConnectionByOid(<parameter>Oid dboid</parameter>, <parameter>Oid useroid</parameter>)</function>.
This allows the process to run transactions and queries using the
- <literal>SPI</literal> interface. If <varname>dbname</> is NULL,
- the session is not connected to any particular database, but shared catalogs
- can be accessed. If <varname>username</> is NULL, the process will run as
- the superuser created during <command>initdb</>.
- BackgroundWorkerInitializeConnection can only be called once per background
- process, it is not possible to switch databases.
+ <literal>SPI</literal> interface. If <varname>dbname</> is NULL or
+ <varname>dboid</> is <literal>InvalidOid</>, the session is not connected
+ to any particular database, but shared catalogs can be accessed.
+ If <varname>username</> is NULL or <varname>useroid</> is
+ <literal>InvalidOid</>, the process will run as the superuser created
+ during <command>initdb</>.
+ A background worker can only call one of these two functions, and only
+ once. It is not possible to switch databases.
</para>
<para>
*/
InitProcess();
- InitPostgres(NULL, InvalidOid, NULL, NULL);
+ InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
/* Initialize stuff for bootstrap-file processing */
for (i = 0; i < MAXATTR; i++)
InitProcess();
#endif
- InitPostgres(NULL, InvalidOid, NULL, NULL);
+ InitPostgres(NULL, InvalidOid, NULL, InvalidOid, NULL);
SetProcessingMode(NormalProcessing);
* Note: if we have selected a just-deleted database (due to using
* stale stats info), we'll fail and exit here.
*/
- InitPostgres(NULL, dbid, NULL, dbname);
+ InitPostgres(NULL, dbid, NULL, InvalidOid, dbname);
SetProcessingMode(NormalProcessing);
set_ps_display(dbname, false);
ereport(DEBUG1,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("database connection requirement not indicated during registration")));
- InitPostgres(dbname, InvalidOid, username, NULL);
+ InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
+
+ /* it had better not gotten out of "init" mode yet */
+ if (!IsInitProcessingMode())
+ ereport(ERROR,
+ (errmsg("invalid processing mode in background worker")));
+ SetProcessingMode(NormalProcessing);
+}
+
+/*
+ * Connect background worker to a database using OIDs.
+ */
+void
+BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid)
+{
+ BackgroundWorker *worker = MyBgworkerEntry;
+
+ /* XXX is this the right errcode? */
+ if (!(worker->bgw_flags & BGWORKER_BACKEND_DATABASE_CONNECTION))
+ ereport(FATAL,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("database connection requirement not indicated during registration")));
+
+ InitPostgres(NULL, dboid, NULL, useroid, NULL);
/* it had better not gotten out of "init" mode yet */
if (!IsInitProcessingMode())
* it inside InitPostgres() instead. In particular, anything that
* involves database access should be there, not here.
*/
- InitPostgres(dbname, InvalidOid, username, NULL);
+ InitPostgres(dbname, InvalidOid, username, InvalidOid, NULL);
/*
* If the PostmasterContext is still around, recycle the space; we don't
* Initialize user identity during normal backend startup
*/
void
-InitializeSessionUserId(const char *rolename)
+InitializeSessionUserId(const char *rolename, Oid roleid)
{
HeapTuple roleTup;
Form_pg_authid rform;
- Oid roleid;
/*
* Don't do scans if we're bootstrapping, none of the system catalogs
/* call only once */
AssertState(!OidIsValid(AuthenticatedUserId));
- roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
+ if (rolename != NULL)
+ roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(rolename));
+ else
+ roleTup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
if (!HeapTupleIsValid(roleTup))
ereport(FATAL,
(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
* name can be returned to the caller in out_dbname. If out_dbname isn't
* NULL, it must point to a buffer of size NAMEDATALEN.
*
+ * Similarly, the username can be passed by name, using the username parameter,
+ * or by OID using the useroid parameter.
+ *
* In bootstrap mode no parameters are used. The autovacuum launcher process
* doesn't use any parameters either, because it only goes far enough to be
* able to read pg_database; it doesn't connect to any particular database.
*/
void
InitPostgres(const char *in_dbname, Oid dboid, const char *username,
- char *out_dbname)
+ Oid useroid, char *out_dbname)
{
bool bootstrap = IsBootstrapProcessingMode();
bool am_superuser;
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("no roles are defined in this database system"),
errhint("You should immediately run CREATE USER \"%s\" SUPERUSER;.",
- username)));
+ username != NULL ? username : "postgres")));
}
else if (IsBackgroundWorker)
{
- if (username == NULL)
+ if (username == NULL && !OidIsValid(useroid))
{
InitializeSessionUserIdStandalone();
am_superuser = true;
}
else
{
- InitializeSessionUserId(username);
+ InitializeSessionUserId(username, useroid);
am_superuser = superuser();
}
}
/* normal multiuser case */
Assert(MyProcPort != NULL);
PerformAuthentication(MyProcPort);
- InitializeSessionUserId(username);
+ InitializeSessionUserId(username, useroid);
am_superuser = superuser();
}
extern bool InSecurityRestrictedOperation(void);
extern void GetUserIdAndContext(Oid *userid, bool *sec_def_context);
extern void SetUserIdAndContext(Oid userid, bool sec_def_context);
-extern void InitializeSessionUserId(const char *rolename);
+extern void InitializeSessionUserId(const char *rolename, Oid useroid);
extern void InitializeSessionUserIdStandalone(void);
extern void SetSessionAuthorization(Oid userid, bool is_superuser);
extern Oid GetCurrentRoleId(void);
extern void pg_split_opts(char **argv, int *argcp, char *optstr);
extern void InitializeMaxBackends(void);
extern void InitPostgres(const char *in_dbname, Oid dboid, const char *username,
- char *out_dbname);
+ Oid useroid, char *out_dbname);
extern void BaseInit(void);
/* in utils/init/miscinit.c */
*/
extern void BackgroundWorkerInitializeConnection(char *dbname, char *username);
+/* Just like the above, but specifying database and user by OID. */
+extern void BackgroundWorkerInitializeConnectionByOid(Oid dboid, Oid useroid);
+
/* Block/unblock signals in a background worker process */
extern void BackgroundWorkerBlockSignals(void);
extern void BackgroundWorkerUnblockSignals(void);