Sub topic 1
How a database server works
A database server is a long-running program that owns a set of files and answers requests from other programs. This chapter describes the pieces involved: the server processes, the memory they share, the files on disk, and the network paths clients use. Later chapters configure each piece. Here the goal is a working model you can use to reason about failures.
Prerequisites
This chapter assumes you can:
- Use the Linux command line and run commands with
sudo. - Read and write basic SQL (
SELECT,INSERT,UPDATE,DELETE). - Explain what an IP address, a port and the difference between
localhostand a network interface are. - Check a service with
systemctland install packages withaptordnf.
The client/server model
A database client is any program that sends SQL and reads results. Examples are psql, mysql, a web application or a backup tool. The server is a separate program that holds the data. The two only communicate through a connection, so the client can run on the same host or on another one.
Every request follows the same path:
- The client opens a connection and sends credentials.
- The server checks whether this client may connect, then creates a session for it.
- The client sends SQL. The server parses it, plans it, and reads or changes data.
- The server returns rows or an error. The session stays open until the client disconnects.
Server processes
The two engines handle sessions differently, which affects how you size and monitor them.
| Aspect | PostgreSQL | MySQL (InnoDB) |
|---|---|---|
| Main program | `postgres` (the postmaster) | `mysqld` |
| Session model | One operating-system process per connection | One thread per connection inside a single process |
| Helper work | Separate background processes (checkpointer, WAL writer, autovacuum) | Background threads in `mysqld` |
| Default port | 5432 | 3306 |
| Cache | `shared_buffers` | InnoDB buffer pool |
In PostgreSQL, the postmaster listens for new connections and starts a backend process for each one. You can see this with ps. The same command shows the background processes.
ps -eo pid,ppid,cmd | grep '[p]ostgres'Backend processes show the postmaster's PID in the ppid column. In MySQL you see only the mysqld process, and the sessions are threads inside it. Use the server's own views instead:
sudo -u postgres psql -c "SELECT pid, usename, datname, state FROM pg_stat_activity;"
sudo mysql -e 'SHOW FULL PROCESSLIST;'A process per connection is why PostgreSQL deployments often add a connection pooler. A later chapter covers connection limits and pooling.
Memory, the write-ahead log and data files
A database does not write every change straight into its table files. Both engines follow the same pattern:
- A change is applied to a page held in a shared memory cache.
- A description of the change is appended to a sequential log first. PostgreSQL calls it the write-ahead log (WAL). InnoDB calls it the redo log.
- The log is flushed to disk before the server tells the client the transaction committed.
- Modified pages are written to the data files later, in batches. PostgreSQL calls this a checkpoint.
The sequential log makes commits fast and safe. If the host loses power, the server replays the log on the next start and recovers committed work. This is also why a database that was stopped uncleanly may spend time in recovery at startup, and why that is normal.
The shared cache is the main memory setting on the host. Reads served from it avoid disk access, so a cache that is too small slows every query. A cache that is too large starves the operating system and per-session memory. The sizing chapter returns to this.
Where the server keeps things
A server has three kinds of files, and you will manage each differently:
- Configuration files hold settings. They are plain text and safe to back up with your other configuration.
- The data directory holds table files, indexes and the log. Never edit it by hand and never copy it while the server runs, unless you use a documented backup method.
- Runtime files include the process ID file and the Unix socket. The server creates and removes them as it starts and stops.
Typical Debian and Ubuntu locations:
| Item | PostgreSQL (version 16 shown) | MySQL |
|---|---|---|
| Configuration | `/etc/postgresql/16/main/` | `/etc/mysql/` |
| Data directory | `/var/lib/postgresql/16/main` | `/var/lib/mysql` |
| Unix socket | `/var/run/postgresql/.s.PGSQL.5432` | `/var/run/mysqld/mysqld.sock` |
Other distributions and vendor packages use different paths, so confirm them on your host. Ask the running server instead of guessing:
sudo -u postgres psql -c "SHOW data_directory;"
sudo mysql -e "SHOW VARIABLES LIKE 'datadir';"Two ways to connect
A client can reach the server over a Unix socket or over TCP.
A Unix socket is a file on the local host. Only programs on the same machine can use it, and the server can see which operating-system user is connecting. TCP uses an address and a port, so it works across the network, but the server then decides access from the client's IP address and credentials.
# Local socket
psql -h /var/run/postgresql -U appuser appdb
mysql --protocol=socket -u appuser -p appdb
# TCP over the loopback interface
psql -h 127.0.0.1 -U appuser appdb
mysql --protocol=tcp -h 127.0.0.1 -u appuser -p appdbBoth connections can succeed or fail independently. A common surprise is that a login works on the server over the socket but fails from the application host, because the server does not listen on the network or no rule allows that address. Check what the server actually listens on:
ss -ltnp | grep -E ':(5432|3306)'A line showing 127.0.0.1:5432 means only local TCP clients can connect. 0.0.0.0 means every IPv4 interface, which is rarely what you want.
Common mistakes
- Treating the server like a library. Applications do not open the data files. Only the server process touches them, and all access goes through connections.
- Copying the data directory from a running server. The copy can be inconsistent and fail to start. Use logical or physical backup tools.
- Assuming a socket test proves network access. Test from the client host over TCP as well.
- Ignoring recovery after a crash. A start that takes a long time after an unclean shutdown is often log replay. Read the logs before restarting again.
- Confusing the database role with the operating-system user.
postgresis both an OS account that owns the files and a database superuser. The two are related but separate.
Exercise
A web application on 10.0.0.20 cannot connect to PostgreSQL on 10.0.0.5. sudo -u postgres psql works on the database host, and ss -ltnp there shows only 127.0.0.1:5432. What is the most likely cause, and which part of the server model does it involve?
Show answer
PostgreSQL listens only on the loopback address, so it accepts local TCP clients but refuses connections from other hosts. The local psql test works because it uses the Unix socket, which never touches the network. The fix is to add the private address to the server's listening addresses (and then allow it in client authentication and the firewall), which later chapters cover.
Exercise
Why can PostgreSQL acknowledge a commit before the changed table pages are written to the data files, and what happens after a power loss?
Show answer
The server first appends the change to the write-ahead log and flushes it to disk, and only then reports success. Table pages are written later at checkpoints. After a power loss, the server replays the log at startup and restores the committed changes.