Heuristic:2ndQuadrant Pglogical Temp Directory Configuration
| Knowledge Sources | |
|---|---|
| Domains | Replication, Infrastructure, Synchronization |
| Last Updated | 2026-10-13 03:51 GMT |
Overview
Configure `pglogical.temp_directory` to a writable path outside PostgreSQL's `temp_tablespace` for schema synchronization dump files, as PostgreSQL crash recovery may delete files in the temp tablespace.
Description
pglogical uses a temporary directory to store `pg_dump` output during initial schema synchronization. The directory is determined by the `pglogical.temp_directory` GUC (default: empty string). When empty, pglogical falls back to the `TMPDIR` environment variable on Unix or `GetTempPath` on Windows, then to `/tmp` if `TMPDIR` is not set. The directory must exist and be writable by the PostgreSQL server user.
pglogical explicitly does NOT use PostgreSQL's `temp_tablespace` for these dumps because PostgreSQL's crash recovery carefully deletes only specifically formatted files from temp tablespaces, which could destroy pglogical's dump files. The GUC is `PGC_SIGHUP`, so it can be changed with a SIGHUP reload without restarting the server.
Usage
Set `pglogical.temp_directory` in `postgresql.conf` when:
- The default `/tmp` is too small for schema dumps.
- The default `/tmp` is on a different filesystem with insufficient space.
- You want dumps on faster storage (SSD) to speed up initial sync.
- You need to avoid `temp_tablespace` locations.
After setting, reload PostgreSQL configuration with `SELECT pg_reload_conf();` or `SIGHUP`.
The Insight (Rule of Thumb)
- Action: Set `pglogical.temp_directory` in `postgresql.conf` to a dedicated, writable directory.
- Value: Absolute path, e.g., `/var/lib/postgresql/pglogical_tmp`. Default: `$TMPDIR` or `/tmp`.
- Trade-off: Schema dumps can be large; ensure adequate disk space on the chosen path. Using a faster filesystem reduces initial sync time.
- Compatibility: Do NOT point this at a PostgreSQL `temp_tablespace` location — crash recovery may delete dump files there.
Reasoning
During initial subscription sync, pglogical dumps the provider's schema to a file (`pglogical-<pid>.dump`) in the temp directory, then restores it to the subscriber. If this file is lost (e.g., by PostgreSQL crash recovery cleaning up a temp tablespace), the sync fails. The code explicitly notes that Pg's crash recovery is "very careful to delete only particularly formatted files" — meaning pglogical's dump files in a temp tablespace could be incorrectly deleted. The assignment hook resolves the directory at GUC assignment time and handles both Unix (`TMPDIR`) and Windows (`GetTempPath`) fallbacks.
Code Evidence
GUC definition — from `pglogical.c:875-888`:
/*
* We can't use the temp_tablespace safely for our dumps, because Pg's
* crash recovery is very careful to delete only particularly formatted
* files. Instead for now just allow user to specify dump storage.
*/
DefineCustomStringVariable("pglogical.temp_directory",
"Directory to store dumps for local restore",
NULL,
&pglogical_temp_directory_config,
"", PGC_SIGHUP,
0,
NULL,
pglogical_temp_directory_assing_hook,
NULL);
Temp directory resolution — from `pglogical.c:772-802`:
static void
pglogical_temp_directory_assing_hook(const char *newval, void *extra)
{
if (strlen(newval))
{
pglogical_temp_directory = strdup(newval);
}
else
{
#ifndef WIN32
const char *tmpdir = getenv("TMPDIR");
if (!tmpdir)
tmpdir = "/tmp";
#else
char tmpdir[MAXPGPATH];
int ret;
ret = GetTempPath(MAXPGPATH, tmpdir);
if (ret == 0 || ret > MAXPGPATH)
{
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("could not locate temporary directory: %s\n",
!ret ? strerror(errno) : "")));
return false;
}
#endif
pglogical_temp_directory = strdup(tmpdir);
}
}
Temp file usage in sync — from `pglogical_sync.c:937-939`:
snprintf(tmpfile, MAXPGPATH, "%s/pglogical-%d.dump",
pglogical_temp_directory, MyProcPid);
canonicalize_path(tmpfile);