56
56
#include <signal.h>
57
57
#include <time.h>
58
58
59
+ #ifdef HAVE_SHM_OPEN
60
+ #include "sys/mman.h"
61
+ #endif
62
+
59
63
#include "mb/pg_wchar.h"
60
64
#include "getaddrinfo.h"
61
65
#include "getopt_long.h"
@@ -150,6 +154,7 @@ static char *pgdata_native;
150
154
/* defaults */
151
155
static int n_connections = 10 ;
152
156
static int n_buffers = 50 ;
157
+ static char * dynamic_shared_memory_type = NULL ;
153
158
154
159
/*
155
160
* Warning messages for authentication methods
@@ -1076,6 +1081,50 @@ set_null_conf(void)
1076
1081
free (path );
1077
1082
}
1078
1083
1084
+ /*
1085
+ * Determine which dynamic shared memory implementation should be used on
1086
+ * this platform. POSIX shared memory is preferable because the default
1087
+ * allocation limits are much higher than the limits for System V on most
1088
+ * systems that support both, but the fact that a platform has shm_open
1089
+ * doesn't guarantee that that call will succeed when attempted. So, we
1090
+ * attempt to reproduce what the postmaster will do when allocating a POSIX
1091
+ * segment in dsm_impl.c; if it doesn't work, we assume it won't work for
1092
+ * the postmaster either, and configure the cluster for System V shared
1093
+ * memory instead.
1094
+ */
1095
+ static char *
1096
+ choose_dsm_implementation (void )
1097
+ {
1098
+ #ifdef HAVE_SHM_OPEN
1099
+ int ntries = 10 ;
1100
+
1101
+ while (ntries > 0 )
1102
+ {
1103
+ uint32 handle ;
1104
+ char name [64 ];
1105
+ int fd ;
1106
+
1107
+ handle = random ();
1108
+ snprintf (name , 64 , "/PostgreSQL.%u" , handle );
1109
+ if ((fd = shm_open (name , O_CREAT | O_RDWR | O_EXCL , 0600 )) != -1 )
1110
+ {
1111
+ close (fd );
1112
+ shm_unlink (name );
1113
+ return "posix" ;
1114
+ }
1115
+ if (errno != EEXIST )
1116
+ break ;
1117
+ -- ntries ;
1118
+ }
1119
+ #endif
1120
+
1121
+ #ifdef WIN32
1122
+ return "windows" ;
1123
+ #else
1124
+ return "sysv" ;
1125
+ #endif
1126
+ }
1127
+
1079
1128
/*
1080
1129
* Determine platform-specific config settings
1081
1130
*
@@ -1157,6 +1206,7 @@ test_config_settings(void)
1157
1206
SYSTEMQUOTE "\"%s\" --boot -x0 %s "
1158
1207
"-c max_connections=%d "
1159
1208
"-c shared_buffers=%d "
1209
+ "-c dynamic_shared_memory_type=none "
1160
1210
"< \"%s\" > \"%s\" 2>&1" SYSTEMQUOTE ,
1161
1211
backend_exec , boot_options ,
1162
1212
n_connections , test_buffs ,
@@ -1171,6 +1221,11 @@ test_config_settings(void)
1171
1221
printf ("%dMB\n" , (n_buffers * (BLCKSZ / 1024 )) / 1024 );
1172
1222
else
1173
1223
printf ("%dkB\n" , n_buffers * (BLCKSZ / 1024 ));
1224
+
1225
+ printf (_ ("selecting dynamic shared memory implementation ... " ));
1226
+ fflush (stdout );
1227
+ dynamic_shared_memory_type = choose_dsm_implementation ();
1228
+ printf ("%s\n" , dynamic_shared_memory_type );
1174
1229
}
1175
1230
1176
1231
/*
@@ -1265,6 +1320,11 @@ setup_config(void)
1265
1320
conflines = replace_token (conflines , "#log_timezone = 'GMT'" , repltok );
1266
1321
}
1267
1322
1323
+ snprintf (repltok , sizeof (repltok ), "dynamic_shared_memory_type = %s" ,
1324
+ dynamic_shared_memory_type );
1325
+ conflines = replace_token (conflines , "#dynamic_shared_memory_type = posix" ,
1326
+ repltok );
1327
+
1268
1328
snprintf (path , sizeof (path ), "%s/postgresql.conf" , pg_data );
1269
1329
1270
1330
writefile (path , conflines );
0 commit comments