@@ -118,14 +118,14 @@ func (c *PGConfig) Print(w io.Writer) error {
118
118
return e .Encode (cfg )
119
119
}
120
120
121
- // SetDefaults WriteDefaults will resolve the default configuration settings and write them to the
121
+ // SetDefaults will resolve the default configuration settings and write them to the
122
122
// internal config file.
123
123
func (c * PGConfig ) SetDefaults () error {
124
124
// The default wal_segment_size in mb
125
125
const walSegmentSize = 16
126
126
127
127
// Calculate total allocated disk in bytes
128
- diskSizeBytes , err := diskSizeInBytes ()
128
+ diskSizeBytes , err := diskSizeInBytes (c . dataDir )
129
129
if err != nil {
130
130
return fmt .Errorf ("failed to fetch disk size: %s" , err )
131
131
}
@@ -166,7 +166,7 @@ func (c *PGConfig) SetDefaults() error {
166
166
sharedPreloadLibraries = append (sharedPreloadLibraries , "timescaledb" )
167
167
}
168
168
169
- conf : = ConfigMap {
169
+ c . internalConfig = ConfigMap {
170
170
"random_page_cost" : "1.1" ,
171
171
"port" : c .port ,
172
172
"shared_buffers" : fmt .Sprintf ("%dMB" , sharedBuffersMb ),
@@ -183,11 +183,37 @@ func (c *PGConfig) SetDefaults() error {
183
183
"shared_preload_libraries" : fmt .Sprintf ("'%s'" , strings .Join (sharedPreloadLibraries , "," )),
184
184
}
185
185
186
- c .internalConfig = conf
186
+ if err := writeInternalConfigFile (c ); err != nil {
187
+ return fmt .Errorf ("failed to write internal config file: %s" , err )
188
+ }
187
189
188
190
return nil
189
191
}
190
192
193
+ func memTotalInBytes () (int64 , error ) {
194
+ memoryStr := os .Getenv ("FLY_VM_MEMORY_MB" )
195
+ if memoryStr == "" {
196
+ return 0 , fmt .Errorf ("FLY_VM_MEMORY_MB envvar has not been set" )
197
+ }
198
+
199
+ parsed , err := strconv .ParseInt (memoryStr , 10 , 64 )
200
+ if err != nil {
201
+ return 0 , err
202
+ }
203
+
204
+ memoryBytes := parsed * (1024 * 1024 )
205
+
206
+ return memoryBytes , nil
207
+ }
208
+
209
+ func diskSizeInBytes (dir string ) (uint64 , error ) {
210
+ var stat syscall.Statfs_t
211
+ if err := syscall .Statfs (dir , & stat ); err != nil {
212
+ return 0 , err
213
+ }
214
+ return stat .Blocks * uint64 (stat .Bsize ), nil
215
+ }
216
+
191
217
func (c * PGConfig ) RuntimeApply (ctx context.Context , conn * pgx.Conn ) error {
192
218
for key , value := range c .userConfig {
193
219
if err := admin .SetConfigurationSetting (ctx , conn , key , value ); err != nil {
@@ -201,24 +227,20 @@ func (c *PGConfig) RuntimeApply(ctx context.Context, conn *pgx.Conn) error {
201
227
// initialize will ensure the required configuration files are stubbed and the parent
202
228
// postgresql.conf file includes them.
203
229
func (c * PGConfig ) initialize () error {
204
- if _ , err := os .Stat (c .internalConfigFilePath ); err != nil {
205
- if os .IsNotExist (err ) {
206
- if _ , err := utils .RunCommand (fmt .Sprintf ("touch %s" , c .internalConfigFilePath ), "postgres" ); err != nil {
207
- return err
208
- }
209
- } else {
210
- return err
211
- }
230
+ if err := stubConfigurationFile (c .internalConfigFilePath ); err != nil {
231
+ return err
212
232
}
213
233
214
- if _ , err := os .Stat (c .userConfigFilePath ); err != nil {
215
- if os .IsNotExist (err ) {
216
- if _ , err := utils .RunCommand (fmt .Sprintf ("touch %s" , c .userConfigFilePath ), "postgres" ); err != nil {
217
- return err
218
- }
219
- } else {
220
- return err
221
- }
234
+ if err := utils .SetFileOwnership (c .internalConfigFilePath , "postgres" ); err != nil {
235
+ return err
236
+ }
237
+
238
+ if err := stubConfigurationFile (c .userConfigFilePath ); err != nil {
239
+ return err
240
+ }
241
+
242
+ if err := utils .SetFileOwnership (c .userConfigFilePath , "postgres" ); err != nil {
243
+ return err
222
244
}
223
245
224
246
b , err := os .ReadFile (c .configFilePath )
@@ -264,26 +286,19 @@ func (c *PGConfig) writePGConfigEntries(entries []string) error {
264
286
return file .Sync ()
265
287
}
266
288
267
- func memTotalInBytes () (int64 , error ) {
268
- memoryStr := os .Getenv ("FLY_VM_MEMORY_MB" )
269
- if memoryStr == "" {
270
- return 0 , fmt .Errorf ("FLY_VM_MEMORY_MB envvar has not been set" )
271
- }
289
+ func stubConfigurationFile (path string ) error {
290
+ _ , err := os .Stat (path )
291
+ if os .IsNotExist (err ) {
292
+ file , err := os .Create (path )
293
+ if err != nil {
294
+ return err
295
+ }
296
+ defer func () { _ = file .Close () }()
272
297
273
- parsed , err := strconv . ParseInt ( memoryStr , 10 , 64 )
274
- if err != nil {
275
- return 0 , err
298
+ return file . Sync ( )
299
+ } else if err != nil {
300
+ return nil
276
301
}
277
302
278
- memoryBytes := parsed * (1024 * 1024 )
279
-
280
- return memoryBytes , nil
281
- }
282
-
283
- func diskSizeInBytes () (uint64 , error ) {
284
- var stat syscall.Statfs_t
285
- if err := syscall .Statfs ("/data" , & stat ); err != nil {
286
- return 0 , err
287
- }
288
- return stat .Blocks * uint64 (stat .Bsize ), nil
303
+ return nil
289
304
}
0 commit comments