Commit ea23ec82 authored by Tom Lane's avatar Tom Lane

Remove GUC USERLIMIT variable category, making the affected variables

plain SUSET instead.  Also delay processing of options received in
client connection request until after we know if the user is a superuser,
so that SUSET values can be set that way by legitimate superusers.
Per recent discussion.
parent 1c7a47ce
<!--
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.291 2004/11/05 19:15:49 tgl Exp $
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.292 2004/11/14 19:35:28 tgl Exp $
-->
<Chapter Id="runtime">
......@@ -2150,7 +2150,7 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
to the log. The default is <literal>NOTICE</>. Note that
<literal>LOG</> has a different rank here than in
<varname>client_min_messages</>.
Only superusers can increase this option.
Only superusers can change this setting.
</para>
</listitem>
</varlistentry>
......@@ -2186,7 +2186,7 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
SQL statements causing errors, fatal errors, or panics will be
logged. Enabling this option can be helpful in tracking down
the source of any errors that appear in the server log.
Only superusers can increase this option.
Only superusers can change this setting.
</para>
</listitem>
</varlistentry>
......@@ -2204,8 +2204,7 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
<literal>250</literal> then all SQL statements that run 250ms
or longer will be logged. Enabling this option can be
useful in tracking down unoptimized queries in your applications.
Only superusers can increase this or set it to minus-one if this
option is set by the administrator.
Only superusers can change this setting.
</para>
</listitem>
</varlistentry>
......@@ -2332,7 +2331,7 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
these displays to produce a more readable but much longer
output format. <varname>client_min_messages</varname> or
<varname>log_min_messages</varname> must be
<literal>DEBUG1</literal> or lower to send output to the
<literal>DEBUG1</literal> or lower to send the output to the
client or server logs. These options are off by default.
</para>
</listitem>
......@@ -2372,10 +2371,9 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
<varname>log_statement</> to be logged. When using this option,
if you are not using <application>syslog</>, it is recommended
that you log the PID or session ID using <varname>log_line_prefix</>
or log the session ID so that you can link the statement to the
so that you can link the statement to the
duration using the process ID or session ID. The default is off.
Only superusers can turn off this option if it is enabled by the
administrator.
Only superusers can change this setting.
</para>
</listitem>
</varlistentry>
......@@ -2487,35 +2485,35 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
<listitem>
<para>
Controls which SQL statements are logged. Valid values are
<literal>all</>, <literal>ddl</>, <literal>mod</>, and
<literal>none</>. <literal>ddl</> logs all data definition
<literal>none</>, <literal>ddl</>, <literal>mod</>, and
<literal>all</>. <literal>ddl</> logs all data definition
commands like <literal>CREATE</>, <literal>ALTER</>, and
<literal>DROP</> commands. <literal>mod</> logs all
<literal>ddl</> statements, plus <literal>INSERT</>,
<literal>UPDATE</>, <literal>DELETE</>, <literal>TRUNCATE</>,
and <literal>COPY FROM</>. <literal>PREPARE</> and
<literal>EXPLAIN ANALYZE</> statements are also considered for
appropriate commands.
<literal>EXPLAIN ANALYZE</> statements are also logged if their
contained command is of an appropriate type.
</para>
<para>
The default is <literal>none</>. Only superusers can reduce
the detail of this option if it has been set by an administrator.
The default is <literal>none</>. Only superusers can change this
setting.
</para>
<note>
<para>
The <command>EXECUTE</command> statement not considered a
The <command>EXECUTE</command> statement is not considered a
<literal>ddl</> or <literal>mod</> statement. When it is logged,
only the name of the prepared statement is reported, not the
actual prepared statement.
</para>
<para>
When a function is defined in a server-side language like
<application>PL/pgSQL</application>, any queries executed by
the function will only be logged the first time that the
When a function is defined in the
<application>PL/pgSQL</application>server-side language, any queries
executed by the function will only be logged the first time that the
function is invoked in a particular session. This is because
the <application>PL/pgSQL</application> keeps a cache of the
<application>PL/pgSQL</application> keeps a cache of the
query plans produced for the SQL statements in the function.
</para>
</note>
......@@ -2556,11 +2554,10 @@ archive_command = 'copy "%p" /mnt/server/archivedir/"%f"' # Win32
For each query, write performance statistics of the respective
module to the server log. This is a crude profiling
instrument. <varname>log_statement_stats</varname> reports total
statement statistics, while the others report per-state statistics.
<varname>log_statement_stats</varname> can not be enabled with
the other options. All of these options are disabled by default.
Only superusers can turn off any of these options if they have
been enabled by the administrator.
statement statistics, while the others report per-module statistics.
<varname>log_statement_stats</varname> cannot be enabled together with
any of the per-module options. All of these options are disabled by
default. Only superusers can change these settings.
</para>
</listitem>
</varlistentry>
......
......@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.195 2004/10/10 23:37:16 neilc Exp $
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.196 2004/11/14 19:35:29 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -424,7 +424,7 @@ BootstrapMain(int argc, char *argv[])
/*
* backend initialization
*/
InitPostgres(dbname, NULL);
(void) InitPostgres(dbname, NULL);
/*
* In NOP mode, all we really want to do is create shared memory and
......
......@@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.437 2004/11/09 13:01:26 petere Exp $
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.438 2004/11/14 19:35:30 tgl Exp $
*
* NOTES
*
......@@ -223,8 +223,6 @@ bool ClientAuthInProgress = false; /* T during new-client
*/
static unsigned int random_seed = 0;
static int debug_flag = 0;
extern char *optarg;
extern int optind,
opterr;
......@@ -401,17 +399,8 @@ PostmasterMain(int argc, char *argv[])
userDoption = optarg;
break;
case 'd':
{
/* Turn on debugging for the postmaster. */
char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
sprintf(debugstr, "debug%s", optarg);
SetConfigOption("log_min_messages", debugstr,
PGC_POSTMASTER, PGC_S_ARGV);
pfree(debugstr);
debug_flag = atoi(optarg);
set_debug_options(atoi(optarg), PGC_POSTMASTER, PGC_S_ARGV);
break;
}
case 'F':
SetConfigOption("fsync", "false", PGC_POSTMASTER, PGC_S_ARGV);
break;
......@@ -2511,7 +2500,6 @@ BackendRun(Port *port)
char **av;
int maxac;
int ac;
char debugbuf[32];
char protobuf[32];
int i;
......@@ -2707,15 +2695,6 @@ BackendRun(Port *port)
av[ac++] = "postgres";
/*
* Pass the requested debugging level along to the backend.
*/
if (debug_flag > 0)
{
snprintf(debugbuf, sizeof(debugbuf), "-d%d", debug_flag);
av[ac++] = debugbuf;
}
/*
* Pass any backend switches specified with -o in the postmaster's own
* command line. We assume these are secure. (It's OK to mangle
......@@ -3404,7 +3383,6 @@ write_backend_variables(char *filename, Port *port)
write_var(ProcStructLock, fp);
write_var(pgStatSock, fp);
write_var(debug_flag, fp);
write_var(PostmasterPid, fp);
#ifdef WIN32
write_var(PostmasterHandle, fp);
......@@ -3478,7 +3456,6 @@ read_backend_variables(char *filename, Port *port)
read_var(ProcStructLock, fp);
read_var(pgStatSock, fp);
read_var(debug_flag, fp);
read_var(PostmasterPid, fp);
#ifdef WIN32
read_var(PostmasterHandle, fp);
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.436 2004/10/15 16:50:31 momjian Exp $
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.437 2004/11/14 19:35:31 tgl Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
......@@ -2151,6 +2151,41 @@ usage(const char *progname)
}
/*
* set_debug_options --- apply "-d N" command line option
*
* -d is not quite the same as setting log_min_messages because it enables
* other output options.
*/
void
set_debug_options(int debug_flag, GucContext context, GucSource source)
{
if (debug_flag > 0)
{
char debugstr[64];
sprintf(debugstr, "debug%d", debug_flag);
SetConfigOption("log_min_messages", debugstr, context, source);
}
else
SetConfigOption("log_min_messages", "notice", context, source);
if (debug_flag >= 1 && context == PGC_POSTMASTER)
{
SetConfigOption("log_connections", "true", context, source);
SetConfigOption("log_disconnections", "true", context, source);
}
if (debug_flag >= 2)
SetConfigOption("log_statement", "all", context, source);
if (debug_flag >= 3)
SetConfigOption("debug_print_parse", "true", context, source);
if (debug_flag >= 4)
SetConfigOption("debug_print_plan", "true", context, source);
if (debug_flag >= 5)
SetConfigOption("debug_print_rewritten", "true", context, source);
}
/* ----------------------------------------------------------------
* PostgresMain
* postgres main loop -- all backends, interactive or otherwise start here
......@@ -2169,10 +2204,12 @@ PostgresMain(int argc, char *argv[], const char *username)
char *userDoption = NULL;
bool secure;
int errs = 0;
int debug_flag = 0;
GucContext ctx,
debug_context;
int debug_flag = -1; /* -1 means not given */
List *guc_names = NIL; /* for possibly-SUSET options */
List *guc_values = NIL;
GucContext ctx;
GucSource gucsource;
bool am_superuser;
char *tmp;
int firstchar;
char stack_base;
......@@ -2180,6 +2217,10 @@ PostgresMain(int argc, char *argv[], const char *username)
sigjmp_buf local_sigjmp_buf;
volatile bool send_rfq = true;
#define PendingConfigOption(name,val) \
(guc_names = lappend(guc_names, pstrdup(name)), \
guc_values = lappend(guc_values, pstrdup(val)))
/*
* Catch standard options before doing much else. This even works on
* systems without getopt_long.
......@@ -2257,10 +2298,11 @@ PostgresMain(int argc, char *argv[], const char *username)
/* all options are allowed until '-p' */
secure = true;
ctx = debug_context = PGC_POSTMASTER;
ctx = PGC_POSTMASTER;
gucsource = PGC_S_ARGV; /* initial switches came from command line */
while ((flag = getopt(argc, argv, "A:B:c:D:d:Eef:FiNOPo:p:S:st:v:W:-:")) != -1)
{
switch (flag)
{
case 'A':
......@@ -2287,40 +2329,7 @@ PostgresMain(int argc, char *argv[], const char *username)
break;
case 'd': /* debug level */
{
/*
* Client option can't decrease debug level. We have
* to do the test here because we group priv and
* client set GUC calls below, after we know the final
* debug value.
*/
if (ctx != PGC_BACKEND || atoi(optarg) > debug_flag)
{
debug_flag = atoi(optarg);
debug_context = ctx; /* save context for use
* below */
/* Set server debugging level. */
if (debug_flag != 0)
{
char *debugstr = palloc(strlen("debug") + strlen(optarg) + 1);
sprintf(debugstr, "debug%s", optarg);
SetConfigOption("log_min_messages", debugstr, ctx, gucsource);
pfree(debugstr);
}
else
/*
* -d0 allows user to prevent postmaster debug
* from propagating to backend. It would be
* nice to set it to the postgresql.conf value
* here.
*/
SetConfigOption("log_min_messages", "notice",
ctx, gucsource);
}
}
break;
case 'E':
......@@ -2448,7 +2457,7 @@ PostgresMain(int argc, char *argv[], const char *username)
/*
* s - report usage statistics (timings) after each query
*/
SetConfigOption("log_statement_stats", "true", ctx, gucsource);
PendingConfigOption("log_statement_stats", "true");
break;
case 't':
......@@ -2481,7 +2490,7 @@ PostgresMain(int argc, char *argv[], const char *username)
break;
}
if (tmp)
SetConfigOption(tmp, "true", ctx, gucsource);
PendingConfigOption(tmp, "true");
break;
case 'v':
......@@ -2518,7 +2527,7 @@ PostgresMain(int argc, char *argv[], const char *username)
optarg)));
}
SetConfigOption(name, value, ctx, gucsource);
PendingConfigOption(name, value);
free(name);
if (value)
free(value);
......@@ -2529,53 +2538,6 @@ PostgresMain(int argc, char *argv[], const char *username)
errs++;
break;
}
/*
* -d is not the same as setting log_min_messages because it enables
* other output options.
*/
if (debug_flag >= 1)
{
SetConfigOption("log_connections", "true", debug_context, gucsource);
SetConfigOption("log_disconnections", "true", debug_context, gucsource);
}
if (debug_flag >= 2)
SetConfigOption("log_statement", "all", debug_context, gucsource);
if (debug_flag >= 3)
SetConfigOption("debug_print_parse", "true", debug_context, gucsource);
if (debug_flag >= 4)
SetConfigOption("debug_print_plan", "true", debug_context, gucsource);
if (debug_flag >= 5)
SetConfigOption("debug_print_rewritten", "true", debug_context, gucsource);
/*
* Process any additional GUC variable settings passed in startup
* packet.
*/
if (MyProcPort != NULL)
{
ListCell *gucopts = list_head(MyProcPort->guc_options);
while (gucopts)
{
char *name;
char *value;
name = lfirst(gucopts);
gucopts = lnext(gucopts);
value = lfirst(gucopts);
gucopts = lnext(gucopts);
SetConfigOption(name, value, PGC_BACKEND, PGC_S_CLIENT);
}
/*
* set up handler to log session end.
*/
if (IsUnderPostmaster && Log_disconnections)
on_proc_exit(log_disconnections, 0);
}
/* Acquire configuration parameters, unless inherited from postmaster */
......@@ -2710,10 +2672,72 @@ PostgresMain(int argc, char *argv[], const char *username)
*/
ereport(DEBUG3,
(errmsg_internal("InitPostgres")));
InitPostgres(dbname, username);
am_superuser = InitPostgres(dbname, username);
SetProcessingMode(NormalProcessing);
/*
* Now that we know if client is a superuser, we can apply GUC options
* that came from the client. (For option switches that are definitely
* not SUSET, we just went ahead and applied them above, but anything
* that is or might be SUSET has to be postponed to here.)
*/
ctx = am_superuser ? PGC_SUSET : PGC_USERSET;
if (debug_flag >= 0)
set_debug_options(debug_flag, ctx, PGC_S_CLIENT);
if (guc_names != NIL)
{
ListCell *namcell,
*valcell;
forboth(namcell, guc_names, valcell, guc_values)
{
char *name = (char *) lfirst(namcell);
char *value = (char *) lfirst(valcell);
SetConfigOption(name, value, ctx, PGC_S_CLIENT);
pfree(name);
pfree(value);
}
}
/*
* Process any additional GUC variable settings passed in startup
* packet.
*/
if (MyProcPort != NULL)
{
ListCell *gucopts = list_head(MyProcPort->guc_options);
while (gucopts)
{
char *name;
char *value;
name = lfirst(gucopts);
gucopts = lnext(gucopts);
value = lfirst(gucopts);
gucopts = lnext(gucopts);
SetConfigOption(name, value, ctx, PGC_S_CLIENT);
}
/*
* set up handler to log session end.
*/
if (IsUnderPostmaster && Log_disconnections)
on_proc_exit(log_disconnections, 0);
}
/*
* Now all GUC states are fully set up. Report them to client if
* appropriate.
*/
BeginReportingGUCOptions();
/*
* Send this backend's cancellation info to the frontend.
*/
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.137 2004/08/29 05:06:51 momjian Exp $
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.138 2004/11/14 19:35:32 tgl Exp $
*
*
*-------------------------------------------------------------------------
......@@ -221,14 +221,21 @@ BaseInit(void)
* InitPostgres
* Initialize POSTGRES.
*
* In bootstrap mode neither of the parameters are used.
*
* The return value indicates whether the userID is a superuser. (That
* can only be tested inside a transaction, so we want to do it during
* the startup transaction rather than doing a separate one in postgres.c.)
*
* Note:
* Be very careful with the order of calls in the InitPostgres function.
* --------------------------------
*/
void
bool
InitPostgres(const char *dbname, const char *username)
{
bool bootstrap = IsBootstrapProcessingMode();
bool am_superuser;
/*
* Set up the global variables holding database id and path.
......@@ -397,16 +404,20 @@ InitPostgres(const char *dbname, const char *username)
*/
RelationCacheInitializePhase3();
/*
* Check if user is a superuser.
*/
if (bootstrap)
am_superuser = true;
else
am_superuser = superuser();
/*
* Check a normal user hasn't connected to a superuser reserved slot.
* We can't do this till after we've read the user information, and we
* must do it inside a transaction since checking superuserness may
* require database access. The superuser check is probably the most
* expensive part; don't do it until necessary.
*/
if (ReservedBackends > 0 &&
CountEmptyBackendSlots() < ReservedBackends &&
!superuser())
*/
if (!am_superuser &&
ReservedBackends > 0 &&
CountEmptyBackendSlots() < ReservedBackends)
ereport(FATAL,
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
errmsg("connection limit exceeded for non-superusers")));
......@@ -422,12 +433,6 @@ InitPostgres(const char *dbname, const char *username)
/* initialize client encoding */
InitializeClientEncoding();
/*
* Now all default states are fully set up. Report them to client if
* appropriate.
*/
BeginReportingGUCOptions();
/*
* Set up process-exit callback to do pre-shutdown cleanup. This
* should be last because we want shmem_exit to call this routine
......@@ -440,6 +445,8 @@ InitPostgres(const char *dbname, const char *username)
/* close the transaction we started above */
if (!bootstrap)
CommitTransactionCommand();
return am_superuser;
}
/*
......
This diff is collapsed.
......@@ -13,7 +13,7 @@
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.172 2004/10/22 22:33:58 tgl Exp $
* $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.173 2004/11/14 19:35:34 tgl Exp $
*
* NOTES
* some of the information in this file should be moved to other files.
......@@ -302,7 +302,7 @@ extern ProcessingMode Mode;
*****************************************************************************/
/* in utils/init/postinit.c */
extern void InitPostgres(const char *dbname, const char *username);
extern bool InitPostgres(const char *dbname, const char *username);
extern void BaseInit(void);
/* in utils/init/miscinit.c */
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.71 2004/08/29 05:06:58 momjian Exp $
* $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.72 2004/11/14 19:35:35 tgl Exp $
*
* OLD COMMENTS
* This file was created so that other c files could get the two
......@@ -33,11 +33,10 @@ extern int max_stack_depth;
typedef enum
{
/* Reverse order so GUC USERLIMIT is easier */
LOGSTMT_ALL, /* log all statements */
LOGSTMT_NONE, /* log no statements */
LOGSTMT_DDL, /* log data definition statements */
LOGSTMT_MOD, /* log modification statements, plus DDL */
LOGSTMT_NONE /* log no statements */
LOGSTMT_ALL /* log all statements */
} LogStmtLevel;
extern LogStmtLevel log_statement;
......@@ -63,5 +62,7 @@ extern void authdie(SIGNAL_ARGS);
extern int PostgresMain(int argc, char *argv[], const char *username);
extern void ResetUsage(void);
extern void ShowUsage(const char *title);
extern void set_debug_options(int debug_flag,
GucContext context, GucSource source);
#endif /* TCOPPROT_H */
......@@ -7,7 +7,7 @@
* Copyright (c) 2000-2004, PostgreSQL Global Development Group
* Written by Peter Eisentraut <peter_e@gmx.net>.
*
* $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.55 2004/11/05 19:16:41 tgl Exp $
* $PostgreSQL: pgsql/src/include/utils/guc.h,v 1.56 2004/11/14 19:35:35 tgl Exp $
*--------------------------------------------------------------------
*/
#ifndef GUC_H
......@@ -48,9 +48,6 @@
* be set in the connection startup packet, because when it is processed
* we don't yet know if the user is a superuser.
*
* USERLIMIT options can only be manipulated in certain ways by
* non-superusers.
*
* USERSET options can be set by anyone any time.
*/
typedef enum
......@@ -60,7 +57,6 @@ typedef enum
PGC_SIGHUP,
PGC_BACKEND,
PGC_SUSET,
PGC_USERLIMIT,
PGC_USERSET
} GucContext;
......@@ -74,9 +70,7 @@ typedef enum
* as the current value. Note that source == PGC_S_OVERRIDE should be
* used when setting a PGC_INTERNAL option.
*
* PGC_S_UNPRIVILEGED isn't actually a source value, but the dividing line
* between privileged and unprivileged sources for USERLIMIT purposes.
* Similarly, PGC_S_INTERACTIVE isn't a real source value, but is the
* PGC_S_INTERACTIVE isn't actually a source value, but is the
* dividing line between "interactive" and "non-interactive" sources for
* error reporting purposes.
*
......@@ -92,7 +86,6 @@ typedef enum
PGC_S_ENV_VAR, /* postmaster environment variable */
PGC_S_FILE, /* postgresql.conf */
PGC_S_ARGV, /* postmaster command line */
PGC_S_UNPRIVILEGED, /* dividing line for USERLIMIT */
PGC_S_DATABASE, /* per-database setting */
PGC_S_USER, /* per-user setting */
PGC_S_CLIENT, /* from client connection request */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment