diff --git a/bundles/postgresql/items.py b/bundles/postgresql/items.py index 1f851d6..6dc83f6 100644 --- a/bundles/postgresql/items.py +++ b/bundles/postgresql/items.py @@ -1,5 +1,8 @@ from bundlewrap.utils.dicts import merge_dict + +version = node.metadata.get('postgresql/version') + directories = { '/var/lib/postgresql': { 'owner': 'postgres', @@ -14,6 +17,22 @@ directories = { } } +files = { + f"/etc/postgresql/{version}/main/conf.d/managed.conf": { + 'content': '\n'.join( + f'{key} = {value}' + for key, value in sorted(node.metadata.get('postgresql/conf').items()) + ) + '\n', + 'owner': 'postgres', + 'group': 'postgres', + 'needed_by': [ + 'svc_systemd:postgresql', + ], + 'triggers': [ + 'svc_systemd:postgresql:restart', + ], + }, +} svc_systemd['postgresql'] = { 'needs': [ diff --git a/bundles/postgresql/metadata.py b/bundles/postgresql/metadata.py index fef64cf..ea70794 100644 --- a/bundles/postgresql/metadata.py +++ b/bundles/postgresql/metadata.py @@ -1,3 +1,6 @@ +import builtins + + root_password = repo.vault.password_for(f'{node.name} postgresql root') defaults = { @@ -12,6 +15,7 @@ defaults = { }, }, 'postgresql': { + 'conf': {}, 'roles': { 'root': { 'password': root_password, @@ -24,6 +28,32 @@ defaults = { } +@metadata_reactor.provides( + 'postgresql/conf', +) +def conf(metadata): + conf = {} + + def limit(value, min=float('-inf'), max=float('inf'), unit=None): + result = int(builtins.max([builtins.min([max, value]), min])) + return str(result) + unit if unit else result + + ram = metadata.get('vm/ram', None) + if ram: + conf['max_connections'] = limit(ram/50, min=100) + conf['shared_buffers'] = limit(ram/20, min=128, unit='MB') + conf['work_mem'] = limit(ram/500, min=4, max=64, unit='MB') + conf['temp_buffers'] = limit(ram/500, min=8, max=64, unit='MB') + conf['effective_cache_size'] = limit(ram/3, min=4096, unit='MB') + conf['maintenance_work_mem'] = limit(ram/50, min=64, unit='MB') + + return { + 'postgresql': { + 'conf': conf, + }, + } + + @metadata_reactor.provides( 'zfs/datasets', )