Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0905e8a

Browse files
committed
Move processing of startup-packet switches and GUC settings into InitPostgres,
to fix the problem that SetClientEncoding needs to be done beforeInitializeClientEncoding, as reported by Zdenek Kotala. We get at leastthe small consolation of being able to remove the bizarre API detail thathad InitPostgres returning whether user is a superuser.
1 parent00e6a16 commit0905e8a

File tree

4 files changed

+91
-95
lines changed

4 files changed

+91
-95
lines changed

‎src/backend/tcop/postgres.c

Lines changed: 9 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.571 2009/08/29 19:26:51 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.572 2009/09/01 00:09:42 tgl Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -2862,7 +2862,7 @@ get_stats_option_name(const char *arg)
28622862
* from the client's startup packet. The latter have the same syntax but
28632863
* may be restricted in what they can do.
28642864
*
2865-
* argv[0] is the program name either way.
2865+
* argv[0] isignored in either case (it's assumed to bethe program name).
28662866
*
28672867
* ctx is PGC_POSTMASTER for secure options, PGC_BACKEND for insecure options
28682868
* coming from the client, or PGC_SUSET for insecure options coming from
@@ -2871,11 +2871,10 @@ get_stats_option_name(const char *arg)
28712871
* Returns the database name extracted from the command line, if any.
28722872
* ----------------------------------------------------------------
28732873
*/
2874-
staticconstchar*
2874+
constchar*
28752875
process_postgres_switches(intargc,char*argv[],GucContextctx)
28762876
{
28772877
constchar*dbname;
2878-
constchar*argv0=argv[0];
28792878
boolsecure= (ctx==PGC_POSTMASTER);
28802879
interrs=0;
28812880
GucSourcegucsource;
@@ -3073,13 +3072,13 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
30733072
ereport(FATAL,
30743073
(errcode(ERRCODE_SYNTAX_ERROR),
30753074
errmsg("invalid command-line arguments for server process"),
3076-
errhint("Try \"%s --help\" for more information.",argv0)));
3075+
errhint("Try \"%s --help\" for more information.",progname)));
30773076
else
30783077
ereport(FATAL,
30793078
(errcode(ERRCODE_SYNTAX_ERROR),
30803079
errmsg("%s: invalid command-line arguments",
3081-
argv0),
3082-
errhint("Try \"%s --help\" for more information.",argv0)));
3080+
progname),
3081+
errhint("Try \"%s --help\" for more information.",progname)));
30833082
}
30843083

30853084
if (argc-optind==1)
@@ -3114,8 +3113,6 @@ int
31143113
PostgresMain(intargc,char*argv[],constchar*username)
31153114
{
31163115
constchar*dbname;
3117-
boolam_superuser;
3118-
GucContextctx;
31193116
intfirstchar;
31203117
charstack_base;
31213118
StringInfoDatainput_message;
@@ -3176,13 +3173,13 @@ PostgresMain(int argc, char *argv[], const char *username)
31763173
ereport(FATAL,
31773174
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
31783175
errmsg("%s: no database nor user name specified",
3179-
argv[0])));
3176+
progname)));
31803177
}
31813178

31823179
/* Acquire configuration parameters, unless inherited from postmaster */
31833180
if (!IsUnderPostmaster)
31843181
{
3185-
if (!SelectConfigFiles(userDoption,argv[0]))
3182+
if (!SelectConfigFiles(userDoption,progname))
31863183
proc_exit(1);
31873184
/* If timezone is not set, determine what the OS uses */
31883185
pg_timezone_initialize();
@@ -3314,7 +3311,7 @@ PostgresMain(int argc, char *argv[], const char *username)
33143311
* it inside InitPostgres() instead. In particular, anything that
33153312
* involves database access should be there, not here.
33163313
*/
3317-
am_superuser=InitPostgres(dbname,InvalidOid,username,NULL);
3314+
InitPostgres(dbname,InvalidOid,username,NULL);
33183315

33193316
/*
33203317
* If the PostmasterContext is still around, recycle the space; we don't
@@ -3331,70 +3328,6 @@ PostgresMain(int argc, char *argv[], const char *username)
33313328

33323329
SetProcessingMode(NormalProcessing);
33333330

3334-
set_ps_display("startup", false);
3335-
3336-
/*
3337-
* Now that we know if client is a superuser, we can try to apply any
3338-
* command-line options passed in the startup packet.
3339-
*/
3340-
ctx=am_superuser ?PGC_SUSET :PGC_BACKEND;
3341-
3342-
if (MyProcPort!=NULL&&
3343-
MyProcPort->cmdline_options!=NULL)
3344-
{
3345-
/*
3346-
* The maximum possible number of commandline arguments that could
3347-
* come from MyProcPort->cmdline_options is (strlen + 1) / 2; see
3348-
* pg_split_opts().
3349-
*/
3350-
char**av;
3351-
intmaxac;
3352-
intac;
3353-
3354-
maxac=2+ (strlen(MyProcPort->cmdline_options)+1) /2;
3355-
3356-
av= (char**)palloc(maxac*sizeof(char*));
3357-
ac=0;
3358-
3359-
av[ac++]=argv[0];
3360-
3361-
/* Note this mangles MyProcPort->cmdline_options */
3362-
pg_split_opts(av,&ac,MyProcPort->cmdline_options);
3363-
3364-
av[ac]=NULL;
3365-
3366-
Assert(ac<maxac);
3367-
3368-
(void)process_postgres_switches(ac,av,ctx);
3369-
}
3370-
3371-
/*
3372-
* Process any additional GUC variable settings passed in startup packet.
3373-
* These are handled exactly like command-line variables.
3374-
*/
3375-
if (MyProcPort!=NULL)
3376-
{
3377-
ListCell*gucopts=list_head(MyProcPort->guc_options);
3378-
3379-
while (gucopts)
3380-
{
3381-
char*name;
3382-
char*value;
3383-
3384-
name=lfirst(gucopts);
3385-
gucopts=lnext(gucopts);
3386-
3387-
value=lfirst(gucopts);
3388-
gucopts=lnext(gucopts);
3389-
3390-
SetConfigOption(name,value,ctx,PGC_S_CLIENT);
3391-
}
3392-
}
3393-
3394-
/* Apply PostAuthDelay as soon as we've read all options */
3395-
if (PostAuthDelay>0)
3396-
pg_usleep(PostAuthDelay*1000000L);
3397-
33983331
/*
33993332
* Now all GUC states are fully set up. Report them to client if
34003333
* appropriate.

‎src/backend/utils/init/postinit.c

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.196 2009/08/31 19:41:00 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.197 2009/09/01 00:09:42 tgl Exp $
1212
*
1313
*
1414
*-------------------------------------------------------------------------
@@ -44,11 +44,13 @@
4444
#include"storage/procsignal.h"
4545
#include"storage/sinvaladt.h"
4646
#include"storage/smgr.h"
47+
#include"tcop/tcopprot.h"
4748
#include"utils/acl.h"
4849
#include"utils/fmgroids.h"
4950
#include"utils/guc.h"
5051
#include"utils/pg_locale.h"
5152
#include"utils/portal.h"
53+
#include"utils/ps_status.h"
5254
#include"utils/snapmgr.h"
5355
#include"utils/syscache.h"
5456
#include"utils/tqual.h"
@@ -217,6 +219,8 @@ PerformAuthentication(Port *port)
217219
(errmsg("connection authorized: user=%s database=%s",
218220
port->user_name,port->database_name)));
219221

222+
set_ps_display("startup", false);
223+
220224
ClientAuthInProgress= false;/* client_min_messages is active now */
221225
}
222226

@@ -256,7 +260,7 @@ CheckMyDatabase(const char *name, bool am_superuser)
256260
* a way to recover from disabling all access to all databases, for
257261
* example "UPDATE pg_database SET datallowconn = false;".
258262
*
259-
* We do not enforce them fortheautovacuum worker processes either.
263+
* We do not enforce them for autovacuum worker processes either.
260264
*/
261265
if (IsUnderPostmaster&& !IsAutoVacuumWorkerProcess())
262266
{
@@ -464,24 +468,20 @@ BaseInit(void)
464468
* doesn't use any parameters either, because it only goes far enough to be
465469
* able to read pg_database; it doesn't connect to any particular database.
466470
*
467-
* The return value indicates whether the userID is a superuser. (That
468-
* can only be tested inside a transaction, so we want to do it during
469-
* the startup transaction rather than doing a separate one in postgres.c.)
470-
*
471471
* As of PostgreSQL 8.2, we expect InitProcess() was already called, so we
472472
* already have a PGPROC struct ... but it's not completely filled in yet.
473473
*
474474
* Note:
475475
*Be very careful with the order of calls in the InitPostgres function.
476476
* --------------------------------
477477
*/
478-
bool
478+
void
479479
InitPostgres(constchar*in_dbname,Oiddboid,constchar*username,
480480
char*out_dbname)
481481
{
482482
boolbootstrap=IsBootstrapProcessingMode();
483-
boolautovacuum=IsAutoVacuumWorkerProcess();
484483
boolam_superuser;
484+
GucContextgucctx;
485485
char*fullpath;
486486
chardbname[NAMEDATALEN];
487487

@@ -558,7 +558,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
558558

559559
/* The autovacuum launcher is done here */
560560
if (IsAutoVacuumLauncherProcess())
561-
return true;/* result doesn't matter */
561+
return;
562562

563563
/*
564564
* Start a new transaction here before first access to db, and get a
@@ -706,12 +706,12 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
706706

707707
/*
708708
* Perform client authentication if necessary, then figure out our
709-
* postgres userid, and see if we are a superuser.
709+
* postgres userID, and see if we are a superuser.
710710
*
711-
* In standalone mode and intheautovacuumprocess, we use a fixed id,
712-
* otherwise we figure it out from the authenticated user name.
711+
* In standalone mode and in autovacuumworker processes, we use a fixed
712+
*ID,otherwise we figure it out from the authenticated user name.
713713
*/
714-
if (bootstrap||autovacuum)
714+
if (bootstrap||IsAutoVacuumWorkerProcess())
715715
{
716716
InitializeSessionUserIdStandalone();
717717
am_superuser= true;
@@ -724,7 +724,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
724724
ereport(WARNING,
725725
(errcode(ERRCODE_UNDEFINED_OBJECT),
726726
errmsg("no roles are defined in this database system"),
727-
errhint("You should immediately run CREATE USER \"%s\"CREATEUSER;.",
727+
errhint("You should immediately run CREATE USER \"%s\"SUPERUSER;.",
728728
username)));
729729
}
730730
else
@@ -768,6 +768,69 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
768768
(errcode(ERRCODE_TOO_MANY_CONNECTIONS),
769769
errmsg("connection limit exceeded for non-superusers")));
770770

771+
/*
772+
* Now process any command-line switches that were included in the startup
773+
* packet, if we are in a regular backend. We couldn't do this before
774+
* because we didn't know if client is a superuser.
775+
*/
776+
gucctx=am_superuser ?PGC_SUSET :PGC_BACKEND;
777+
778+
if (MyProcPort!=NULL&&
779+
MyProcPort->cmdline_options!=NULL)
780+
{
781+
/*
782+
* The maximum possible number of commandline arguments that could
783+
* come from MyProcPort->cmdline_options is (strlen + 1) / 2; see
784+
* pg_split_opts().
785+
*/
786+
char**av;
787+
intmaxac;
788+
intac;
789+
790+
maxac=2+ (strlen(MyProcPort->cmdline_options)+1) /2;
791+
792+
av= (char**)palloc(maxac*sizeof(char*));
793+
ac=0;
794+
795+
av[ac++]="postgres";
796+
797+
/* Note this mangles MyProcPort->cmdline_options */
798+
pg_split_opts(av,&ac,MyProcPort->cmdline_options);
799+
800+
av[ac]=NULL;
801+
802+
Assert(ac<maxac);
803+
804+
(void)process_postgres_switches(ac,av,gucctx);
805+
}
806+
807+
/*
808+
* Process any additional GUC variable settings passed in startup packet.
809+
* These are handled exactly like command-line variables.
810+
*/
811+
if (MyProcPort!=NULL)
812+
{
813+
ListCell*gucopts=list_head(MyProcPort->guc_options);
814+
815+
while (gucopts)
816+
{
817+
char*name;
818+
char*value;
819+
820+
name=lfirst(gucopts);
821+
gucopts=lnext(gucopts);
822+
823+
value=lfirst(gucopts);
824+
gucopts=lnext(gucopts);
825+
826+
SetConfigOption(name,value,gucctx,PGC_S_CLIENT);
827+
}
828+
}
829+
830+
/* Apply PostAuthDelay as soon as we've read all options */
831+
if (PostAuthDelay>0)
832+
pg_usleep(PostAuthDelay*1000000L);
833+
771834
/*
772835
* Initialize various default states that can't be set up until we've
773836
* selected the active user and gotten the right GUC settings.
@@ -786,8 +849,6 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
786849
/* close the transaction we started above */
787850
if (!bootstrap)
788851
CommitTransactionCommand();
789-
790-
returnam_superuser;
791852
}
792853

793854

‎src/include/miscadmin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1414
* Portions Copyright (c) 1994, Regents of the University of California
1515
*
16-
* $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.213 2009/08/29 19:26:51 tgl Exp $
16+
* $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.214 2009/09/01 00:09:42 tgl Exp $
1717
*
1818
* NOTES
1919
* some of the information in this file should be moved to other files.
@@ -324,7 +324,7 @@ extern ProcessingMode Mode;
324324

325325
/* in utils/init/postinit.c */
326326
externvoidpg_split_opts(char**argv,int*argcp,char*optstr);
327-
externboolInitPostgres(constchar*in_dbname,Oiddboid,constchar*username,
327+
externvoidInitPostgres(constchar*in_dbname,Oiddboid,constchar*username,
328328
char*out_dbname);
329329
externvoidBaseInit(void);
330330

‎src/include/tcop/tcopprot.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.99 2009/08/29 19:26:52 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/tcop/tcopprot.h,v 1.100 2009/09/01 00:09:42 tgl Exp $
1111
*
1212
* OLD COMMENTS
1313
* This file was created so that other c files could get the two
@@ -62,6 +62,8 @@ extern void StatementCancelHandler(SIGNAL_ARGS);
6262
externvoidFloatExceptionHandler(SIGNAL_ARGS);
6363
externvoidprepare_for_client_read(void);
6464
externvoidclient_read_ended(void);
65+
externconstchar*process_postgres_switches(intargc,char*argv[],
66+
GucContextctx);
6567
externintPostgresMain(intargc,char*argv[],constchar*username);
6668
externlongget_stack_depth_rlimit(void);
6769
externvoidResetUsage(void);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp