Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion actions/setup-postgres/.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
POSTGRES_USER=postgres
POSTGRES_OPTIONS="-c max_connections=1000 -c shared_buffers=2GB -c log_lock_waits=true"
POSTGRES_PASSWORD=postgres
POSTGRES_DB=chainlink_test
POSTGRES_HOST_AUTH_METHOD=trust

# Default values
POSTGRES_EFFECTIVE_CACHE_SIZE=4GB
POSTGRES_FSYNC=on
POSTGRES_SYNCHRONOUS_COMMIT=on
POSTGRES_CHECKPOINT_TIMEOUT=5min

# Not PG defaults but have been action defaults for awwhile.
POSTGRES_LOG_LOCK_WAITS=on
POSTGRES_MAX_CONNECTIONS=1000
POSTGRES_SHARED_BUFFERS=2GB

# Optionally optimized values will be appended to the file
# taking precedence over the default values.
16 changes: 16 additions & 0 deletions actions/setup-postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,19 @@ directory (absolute path).
print-logs: 'true'
print-logs-path: ${{ github.workspace }}
```

## Performance

Use `tmpfs` (true/false) and `optimize-performance` (true/false) to optimize the
performance of this **testing** database.

### tmpfs

Uses a `tmpfs` (ram disk) for the postgres database.

### optimize-performance

Attempts to maximize performance (reducing durability) based on the current
runner's resources.

See `optimize.sh` for more information.
33 changes: 31 additions & 2 deletions actions/setup-postgres/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@ inputs:
The directory to print the logs to.
Note: This input does nothing if `print-logs` is not set to true.
default: ${{ github.workspace }}

# Postgres specific inputs, tunes performance
tmpfs:
description: |
Whether to use a tmpfs mount for the postgres data directory.
default: "false"
required: false
optimize-performance:
description: |
Whether to optimize the postgres performance settings based on the host resources.
This sets a few parameters that greatly favour performance over durability.
Only use in CI or local testing environments.
default: "false"
required: false

outputs:
database-url:
Expand All @@ -30,9 +39,26 @@ runs:
steps:
- name: Print Inputs
shell: bash
env:
INPUTS_PRINT_LOGS: ${{ inputs.print-logs }}
INPUTS_PRINT_LOGS_PATH: ${{ inputs.print-logs-path }}
INPUTS_TMPFS: ${{ inputs.tmpfs }}
INPUTS_OPTIMIZE_PERFORMANCE: ${{ inputs.optimize-performance }}
run: |
echo "print-logs: ${{ inputs.print-logs }}"
echo "print-logs-path: ${{ inputs.print-logs-path }}"
echo "Print Logs: $INPUTS_PRINT_LOGS"
echo "Print Logs Path: $INPUTS_PRINT_LOGS_PATH"

echo "Use tmpfs: $INPUTS_TMPFS"
echo "Optimize performance: $INPUTS_OPTIMIZE_PERFORMANCE"

- name: Configure Postgres Settings Based on Host Resources
if:
${{ inputs.print-logs != 'true' && inputs.optimize-performance == 'true'
}}
shell: bash
working-directory: ${{ github.action_path }}
run: |
./optimize.sh

- name: Start postgres service
if: ${{ inputs.print-logs != 'true' }}
Expand All @@ -46,6 +72,9 @@ runs:
cp docker-compose.tmpfs.yml docker-compose.override.yml
fi

echo "-- Docker Compose config --"
docker compose config

docker compose up -d

- name: Wait for postgres service to be healthy
Expand Down
8 changes: 7 additions & 1 deletion actions/setup-postgres/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ services:
- "5432:5432"
container_name: cl_pg
image: postgres:14-alpine
command: postgres ${POSTGRES_OPTIONS}
command: >
postgres -c max_connections=${POSTGRES_MAX_CONNECTIONS} -c
shared_buffers=${POSTGRES_SHARED_BUFFERS} -c
effective_cache_size=${POSTGRES_EFFECTIVE_CACHE_SIZE} -c
log_lock_waits=${POSTGRES_LOG_LOCK_WAITS} -c fsync=${POSTGRES_FSYNC} -c
synchronous_commit=${POSTGRES_SYNCHRONOUS_COMMIT} -c
checkpoint_timeout=${POSTGRES_CHECKPOINT_TIMEOUT}
env_file:
- .env
healthcheck:
Expand Down
30 changes: 30 additions & 0 deletions actions/setup-postgres/optimize.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

total_mem_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
total_mem_mb=$((total_mem_kb / 1024))
total_mem_gb=$((total_mem_mb / 1024))

echo "System Memory: ${total_mem_gb}GB (${total_mem_mb}MB)"

shared_buffers="1GB"
effective_cache_size="1GB"

if [ $total_mem_gb -ge 128 ]; then
shared_buffers="16GB"
effective_cache_size="32GB"
elif [ $total_mem_gb -ge 64 ]; then
shared_buffers="8GB"
effective_cache_size="16GB"
elif [ $total_mem_gb -ge 32 ]; then
shared_buffers="4GB"
effective_cache_size="8GB"
elif [ $total_mem_gb -ge 16 ]; then
shared_buffers="2GB"
effective_cache_size="2GB"
fi

echo "POSTGRES_FSYNC=off" >> .env
echo "POSTGRES_SYNCHRONOUS_COMMIT=off" >> .env
echo "POSTGRES_EFFECTIVE_CACHE_SIZE=${effective_cache_size}" >> .env
echo "POSTGRES_SHARED_BUFFERS=${shared_buffers}" >> .env
echo "POSTGRES_CHECKPOINT_TIMEOUT=30min" >> .env
Loading