Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 418039d

Browse files
committed
Process options from the startup packed in walsender. Only few options
make sense for walsender, but for example application_name and client_encoding do. We still don't apply per-role settings from pg_db_role_setting, because that would require connecting to a database to read the table. Fujii Masao
1 parent df57a5e commit 418039d

File tree

1 file changed

+80
-51
lines changed

1 file changed

+80
-51
lines changed

src/backend/utils/init/postinit.c

Lines changed: 80 additions & 51 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.213 2010/07/06 19:18:58 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/init/postinit.c,v 1.214 2010/09/13 09:00:30 heikki Exp $
1212
*
1313
*
1414
*-------------------------------------------------------------------------
@@ -65,6 +65,7 @@ static void CheckMyDatabase(const char *name, bool am_superuser);
6565
static void InitCommunication(void);
6666
static void ShutdownPostgres(int code, Datum arg);
6767
static bool ThereIsAtLeastOneRole(void);
68+
static void process_startup_options(Port *port, bool am_superuser);
6869
static void process_settings(Oid databaseid, Oid roleid);
6970

7071

@@ -476,7 +477,6 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
476477
{
477478
bool bootstrap = IsBootstrapProcessingMode();
478479
bool am_superuser;
479-
GucContext gucctx;
480480
char *fullpath;
481481
char dbname[NAMEDATALEN];
482482

@@ -650,21 +650,37 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
650650
errmsg("remaining connection slots are reserved for non-replication superuser connections")));
651651

652652
/*
653-
* If walsender, we're done here --- we don't want to connect to any
654-
* particular database.
653+
* If walsender, we don't want to connect to any particular database.
654+
* Just finish the backend startup by processing any options from the
655+
* startup packet, and we're done.
655656
*/
656657
if (am_walsender)
657658
{
658659
Assert(!bootstrap);
660+
659661
/* must have authenticated as a superuser */
660662
if (!am_superuser)
661663
ereport(FATAL,
662664
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
663665
errmsg("must be superuser to start walsender")));
666+
667+
/* process any options passed in the startup packet */
668+
if (MyProcPort != NULL)
669+
process_startup_options(MyProcPort, am_superuser);
670+
671+
/* Apply PostAuthDelay as soon as we've read all options */
672+
if (PostAuthDelay > 0)
673+
pg_usleep(PostAuthDelay * 1000000L);
674+
675+
/* initialize client encoding */
676+
InitializeClientEncoding();
677+
664678
/* report this backend in the PgBackendStatus array */
665679
pgstat_bestart();
680+
666681
/* close the transaction we started above */
667682
CommitTransactionCommand();
683+
668684
return;
669685
}
670686

@@ -811,33 +827,76 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
811827
CheckMyDatabase(dbname, am_superuser);
812828

813829
/*
814-
* Now process any command-line switches that were included in the startup
815-
* packet, if we are in a regular backend. We couldn't do this before
830+
* Now process any command-line switches and any additional GUC variable
831+
* settings passed in the startup packet. We couldn't do this before
816832
* because we didn't know if client is a superuser.
817833
*/
834+
if (MyProcPort != NULL)
835+
process_startup_options(MyProcPort, am_superuser);
836+
837+
/* Process pg_db_role_setting options */
838+
process_settings(MyDatabaseId, GetSessionUserId());
839+
840+
/* Apply PostAuthDelay as soon as we've read all options */
841+
if (PostAuthDelay > 0)
842+
pg_usleep(PostAuthDelay * 1000000L);
843+
844+
/*
845+
* Initialize various default states that can't be set up until we've
846+
* selected the active user and gotten the right GUC settings.
847+
*/
848+
849+
/* set default namespace search path */
850+
InitializeSearchPath();
851+
852+
/* initialize client encoding */
853+
InitializeClientEncoding();
854+
855+
/* report this backend in the PgBackendStatus array */
856+
if (!bootstrap)
857+
pgstat_bestart();
858+
859+
/* close the transaction we started above */
860+
if (!bootstrap)
861+
CommitTransactionCommand();
862+
}
863+
864+
/*
865+
* Process any command-line switches and any additional GUC variable
866+
* settings passed in the startup packet.
867+
*/
868+
static void
869+
process_startup_options(Port *port, bool am_superuser)
870+
{
871+
GucContext gucctx;
872+
ListCell *gucopts;
873+
818874
gucctx = am_superuser ? PGC_SUSET : PGC_BACKEND;
819875

820-
if (MyProcPort != NULL &&
821-
MyProcPort->cmdline_options != NULL)
876+
/*
877+
* First process any command-line switches that were included in the
878+
* startup packet, if we are in a regular backend.
879+
*/
880+
if (port->cmdline_options != NULL)
822881
{
823882
/*
824883
* The maximum possible number of commandline arguments that could
825-
* come from MyProcPort->cmdline_options is (strlen + 1) / 2; see
884+
* come from port->cmdline_options is (strlen + 1) / 2; see
826885
* pg_split_opts().
827886
*/
828887
char **av;
829888
int maxac;
830889
int ac;
831890

832-
maxac = 2 + (strlen(MyProcPort->cmdline_options) + 1) / 2;
891+
maxac = 2 + (strlen(port->cmdline_options) + 1) / 2;
833892

834893
av = (char **) palloc(maxac * sizeof(char *));
835894
ac = 0;
836895

837896
av[ac++] = "postgres";
838897

839-
/* Note this mangles MyProcPort->cmdline_options */
840-
pg_split_opts(av, &ac, MyProcPort->cmdline_options);
898+
/* Note this mangles port->cmdline_options */
899+
pg_split_opts(av, &ac, port->cmdline_options);
841900

842901
av[ac] = NULL;
843902

@@ -850,50 +909,20 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
850909
* Process any additional GUC variable settings passed in startup packet.
851910
* These are handled exactly like command-line variables.
852911
*/
853-
if (MyProcPort != NULL)
912+
gucopts = list_head(port->guc_options);
913+
while (gucopts)
854914
{
855-
ListCell *gucopts = list_head(MyProcPort->guc_options);
856-
857-
while (gucopts)
858-
{
859-
char *name;
860-
char *value;
915+
char *name;
916+
char *value;
861917

862-
name = lfirst(gucopts);
863-
gucopts = lnext(gucopts);
918+
name = lfirst(gucopts);
919+
gucopts = lnext(gucopts);
864920

865-
value = lfirst(gucopts);
866-
gucopts = lnext(gucopts);
921+
value = lfirst(gucopts);
922+
gucopts = lnext(gucopts);
867923

868-
SetConfigOption(name, value, gucctx, PGC_S_CLIENT);
869-
}
924+
SetConfigOption(name, value, gucctx, PGC_S_CLIENT);
870925
}
871-
872-
/* Process pg_db_role_setting options */
873-
process_settings(MyDatabaseId, GetSessionUserId());
874-
875-
/* Apply PostAuthDelay as soon as we've read all options */
876-
if (PostAuthDelay > 0)
877-
pg_usleep(PostAuthDelay * 1000000L);
878-
879-
/*
880-
* Initialize various default states that can't be set up until we've
881-
* selected the active user and gotten the right GUC settings.
882-
*/
883-
884-
/* set default namespace search path */
885-
InitializeSearchPath();
886-
887-
/* initialize client encoding */
888-
InitializeClientEncoding();
889-
890-
/* report this backend in the PgBackendStatus array */
891-
if (!bootstrap)
892-
pgstat_bestart();
893-
894-
/* close the transaction we started above */
895-
if (!bootstrap)
896-
CommitTransactionCommand();
897926
}
898927

899928
/*

0 commit comments

Comments
 (0)