Unix socket dir, an idea - Mailing list pgsql-hackers

From Danylo Hlynskyi
Subject Unix socket dir, an idea
Date
Msg-id CANZg+yd2OdqxM=HwnrkT-QecNMqLKNp+bGO_2SirJGznGK6h_w@mail.gmail.com
Whole thread Raw
Responses Re: Unix socket dir, an idea
List pgsql-hackers
Before writing a patch, I'd like to hear discussion first.

I've searched archives first and read following previous discussions on this topic:
- https://www.postgresql.org/message-id/4FCF6040.5030408%40redhat.com
- https://www.postgresql.org/message-id/14899.974513046%40sss.pgh.pa.us

The problem (as I see it) is that everybody would like to move `/tmp` socket dir to `/var/run`, or even `/var/run/postgresql` (or even `/run/postgresql`), but compatibility with old clients (which connect to /tmp by default) is a concern.

One reason to make this move is because any user can create PG socket in `/tmp`, and thus local clients will use that PG, instead of system one (which won't even start because it can't create socket - it is already used).

I propose following 2 ideas:

1. Add a Makefile parameter CONFIG_PGSOCKET_DIR to explicitly switch to new unix socket directory, and let distributions decide if they want this, and if they want, they should handle socket dir change on their own. For example, switch to `/var/run/postgresql` require `/var/run/postgresql` directory to be created - an extra step compared to current situation.

This will allow remove some patches for many (many?) distributions.

By default (CONFIG_PGSOCKET_DIR undefined) unix socket dir should be set to `/tmp` - backward compatibility.

2. The new socket directory shouldn't be hardcoded to single directory, instead it should be detected dynamically.

For server:
- if unix_socket_directory specified, use it
- if not, check if /run/user/$(id -u) exists. If yes, use it as socket dir
- if doesn't exist, check CONFIG_PGSOCKET_DIR exists. If yes, use it as socket dir
- else fail

For client:
- if host explicitly set, use it
- if not, check if /run/user/$(id -u) exists and socket file exists there. If yes, use it as socket
- if doesn't exist, check CONFIG_PGSOCKET_DIR exists. If yes, use it as socket dir
- else fail

What will be solved:
- no more local /tmp hijack
- `pg_ctl start` and psql, when run as single user, will use same socket directory /run/user/$(id -u) - no need to create safe directory first
- psql will still be able to connect to "service" PG - if socket is not found in user runtime dir, then lookup in system (runtime) dir

Drawbacks:
- running pg_ctl as root will no longer make server accessible by default to other users, because /run/user/0 is readable only by root
- if `postgres` user, under which postgresql service runs, is "normal" user, and has /run/user/XXX directory, pg will require start-time -k /var/run/postgresql switch, to be accessible to other users' clients
- there will no longer be a "single" directory to lookup sockets, so an instructions on nuances of unix socket dir resolution for newcomers is required
- non-systemd distributions won't benefit from this logic
- /run/user/$(id -u) is opinionated. $XDG_RUNTIME_DIR would be better

Thoughts?

pgsql-hackers by date:

Previous
From: Christoph Berg
Date:
Subject: Re: PostgreSQL pollutes the file system
Next
From: Andrew Dunstan
Date:
Subject: Re: PostgreSQL pollutes the file system