summaryrefslogtreecommitdiff
path: root/src/fe_utils/recovery_gen.c
blob: e9023584768d5c66cd53ead68d5e709ee7d08ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-------------------------------------------------------------------------
 *
 * recovery_gen.c
 *		Generator for recovery configuration
 *
 * Portions Copyright (c) 2011-2025, PostgreSQL Global Development Group
 *
 *-------------------------------------------------------------------------
 */
#include "postgres_fe.h"

#include "common/logging.h"
#include "fe_utils/recovery_gen.h"
#include "fe_utils/string_utils.h"

static char *escape_quotes(const char *src);
static char *FindDbnameInConnOpts(PQconninfoOption *conn_opts);

/*
 * Write recovery configuration contents into a fresh PQExpBuffer, and
 * return it.
 *
 * This accepts the dbname which will be appended to the primary_conninfo.
 * The dbname will be ignored by walreceiver process but slotsync worker uses
 * it to connect to the primary server.
 */
PQExpBuffer
GenerateRecoveryConfig(PGconn *pgconn, const char *replication_slot,
					   char *dbname)
{
	PQconninfoOption *connOptions;
	PQExpBufferData conninfo_buf;
	char	   *escaped;
	PQExpBuffer contents;

	Assert(pgconn != NULL);

	contents = createPQExpBuffer();
	if (!contents)
		pg_fatal("out of memory");

	/*
	 * In PostgreSQL 12 and newer versions, standby_mode is gone, replaced by
	 * standby.signal to trigger a standby state at recovery.
	 */
	if (PQserverVersion(pgconn) < MINIMUM_VERSION_FOR_RECOVERY_GUC)
		appendPQExpBufferStr(contents, "standby_mode = 'on'\n");

	connOptions = PQconninfo(pgconn);
	if (connOptions == NULL)
		pg_fatal("out of memory");

	initPQExpBuffer(&conninfo_buf);
	for (PQconninfoOption *opt = connOptions; opt && opt->keyword; opt++)
	{
		/* Omit empty settings and those libpqwalreceiver overrides. */
		if (strcmp(opt->keyword, "replication") == 0 ||
			strcmp(opt->keyword, "dbname") == 0 ||
			strcmp(opt->keyword, "fallback_application_name") == 0 ||
			(opt->val == NULL) ||
			(opt->val != NULL && opt->val[0] == '\0'))
			continue;

		/* Separate key-value pairs with spaces */
		if (conninfo_buf.len != 0)
			appendPQExpBufferChar(&conninfo_buf, ' ');

		/*
		 * Write "keyword=value" pieces, the value string is escaped and/or
		 * quoted if necessary.
		 */
		appendPQExpBuffer(&conninfo_buf, "%s=", opt->keyword);
		appendConnStrVal(&conninfo_buf, opt->val);
	}

	if (dbname)
	{
		/*
		 * If dbname is specified in the connection, append the dbname. This
		 * will be used later for logical replication slot synchronization.
		 */
		if (conninfo_buf.len != 0)
			appendPQExpBufferChar(&conninfo_buf, ' ');

		appendPQExpBuffer(&conninfo_buf, "%s=", "dbname");
		appendConnStrVal(&conninfo_buf, dbname);
	}

	if (PQExpBufferDataBroken(conninfo_buf))
		pg_fatal("out of memory");

	/*
	 * Escape the connection string, so that it can be put in the config file.
	 * Note that this is different from the escaping of individual connection
	 * options above!
	 */
	escaped = escape_quotes(conninfo_buf.data);
	termPQExpBuffer(&conninfo_buf);
	appendPQExpBuffer(contents, "primary_conninfo = '%s'\n", escaped);
	free(escaped);

	if (replication_slot)
	{
		/* unescaped: ReplicationSlotValidateName allows [a-z0-9_] only */
		appendPQExpBuffer(contents, "primary_slot_name = '%s'\n",
						  replication_slot);
	}

	if (PQExpBufferBroken(contents))
		pg_fatal("out of memory");

	PQconninfoFree(connOptions);

	return contents;
}

/*
 * Write the configuration file in the directory specified in target_dir,
 * with the contents already collected in memory appended.  Then write
 * the signal file into the target_dir.  If the server does not support
 * recovery parameters as GUCs, the signal file is not necessary, and
 * configuration is written to recovery.conf.
 */
void
WriteRecoveryConfig(PGconn *pgconn, const char *target_dir, PQExpBuffer contents)
{
	char		filename[MAXPGPATH];
	FILE	   *cf;
	bool		use_recovery_conf;

	Assert(pgconn != NULL);

	use_recovery_conf =
		PQserverVersion(pgconn) < MINIMUM_VERSION_FOR_RECOVERY_GUC;

	snprintf(filename, MAXPGPATH, "%s/%s", target_dir,
			 use_recovery_conf ? "recovery.conf" : "postgresql.auto.conf");

	cf = fopen(filename, use_recovery_conf ? "w" : "a");
	if (cf == NULL)
		pg_fatal("could not open file \"%s\": %m", filename);

	if (fwrite(contents->data, contents->len, 1, cf) != 1)
		pg_fatal("could not write to file \"%s\": %m", filename);

	fclose(cf);

	if (!use_recovery_conf)
	{
		snprintf(filename, MAXPGPATH, "%s/%s", target_dir, "standby.signal");
		cf = fopen(filename, "w");
		if (cf == NULL)
			pg_fatal("could not create file \"%s\": %m", filename);

		fclose(cf);
	}
}

/*
 * Escape a string so that it can be used as a value in a key-value pair
 * a configuration file.
 */
static char *
escape_quotes(const char *src)
{
	char	   *result = escape_single_quotes_ascii(src);

	if (!result)
		pg_fatal("out of memory");
	return result;
}

/*
 * FindDbnameInConnOpts
 *
 * This is a helper function for GetDbnameFromConnectionOptions(). Extract
 * the value of dbname from PQconninfoOption parameters, if it's present.
 * Returns a strdup'd result or NULL.
 */
static char *
FindDbnameInConnOpts(PQconninfoOption *conn_opts)
{
	for (PQconninfoOption *conn_opt = conn_opts;
		 conn_opt->keyword != NULL;
		 conn_opt++)
	{
		if (strcmp(conn_opt->keyword, "dbname") == 0 &&
			conn_opt->val != NULL && conn_opt->val[0] != '\0')
			return pg_strdup(conn_opt->val);
	}
	return NULL;
}

/*
 * GetDbnameFromConnectionOptions
 *
 * This is a special purpose function to retrieve the dbname from either the
 * 'connstr' specified by the caller or from the environment variables.
 *
 * Returns NULL, if dbname is not specified by the user in the given
 * connection options.
 */
char *
GetDbnameFromConnectionOptions(const char *connstr)
{
	PQconninfoOption *conn_opts;
	char	   *err_msg = NULL;
	char	   *dbname;

	/* First try to get the dbname from connection string. */
	if (connstr)
	{
		conn_opts = PQconninfoParse(connstr, &err_msg);
		if (conn_opts == NULL)
			pg_fatal("%s", err_msg);

		dbname = FindDbnameInConnOpts(conn_opts);

		PQconninfoFree(conn_opts);
		if (dbname)
			return dbname;
	}

	/*
	 * Next try to get the dbname from default values that are available from
	 * the environment.
	 */
	conn_opts = PQconndefaults();
	if (conn_opts == NULL)
		pg_fatal("out of memory");

	dbname = FindDbnameInConnOpts(conn_opts);

	PQconninfoFree(conn_opts);
	return dbname;
}