Skip to content

Commit 617398e

Browse files
Nishant-at-EDBCommitfest Bot
authored and
Commitfest Bot
committed
Disallow empty Foreign Table column_name, schema_name, table_name options for postgres_fdw.
1 parent 0e3e0ec commit 617398e

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

+24
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ CREATE FOREIGN TABLE ft7 (
139139
c2 int NOT NULL,
140140
c3 text
141141
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4');
142+
CREATE FOREIGN TABLE ft8 (
143+
c1 int OPTIONS (column_name '') NOT NULL,
144+
c2 int NOT NULL,
145+
c3 text
146+
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4');
147+
ERROR: value for option "column_name" must not be empty string
148+
CREATE FOREIGN TABLE ft8 (
149+
c1 int NOT NULL,
150+
c2 int NOT NULL,
151+
c3 text
152+
) SERVER loopback3 OPTIONS (schema_name '', table_name 'T 4');
153+
ERROR: value for option "schema_name" must not be empty string
154+
CREATE FOREIGN TABLE ft8 (
155+
c1 int NOT NULL,
156+
c2 int NOT NULL,
157+
c3 text
158+
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name '');
159+
ERROR: value for option "table_name" must not be empty string
142160
-- ===================================================================
143161
-- tests for validator
144162
-- ===================================================================
@@ -196,10 +214,16 @@ ALTER USER MAPPING FOR public SERVER testserver1
196214
-- permitted to check validation.
197215
ALTER USER MAPPING FOR public SERVER testserver1
198216
OPTIONS (ADD sslkey 'value', ADD sslcert 'value');
217+
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name '', table_name 'T 1');
218+
ERROR: value for option "schema_name" must not be empty string
219+
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name 'S 1', table_name '');
220+
ERROR: value for option "table_name" must not be empty string
199221
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name 'S 1', table_name 'T 1');
200222
ALTER FOREIGN TABLE ft2 OPTIONS (schema_name 'S 1', table_name 'T 1');
201223
ALTER FOREIGN TABLE ft1 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
202224
ALTER FOREIGN TABLE ft2 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
225+
ALTER FOREIGN TABLE ft2 ALTER COLUMN c2 OPTIONS (column_name '');
226+
ERROR: value for option "column_name" must not be empty string
203227
\det+
204228
List of foreign tables
205229
Schema | Table | Server | FDW options | Description

contrib/postgres_fdw/option.c

+16
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,22 @@ postgres_fdw_validator(PG_FUNCTION_ARGS)
227227
errmsg("invalid value for string option \"%s\": %s",
228228
def->defname, value)));
229229
}
230+
else if (strcmp(def->defname, "column_name") == 0 ||
231+
strcmp(def->defname, "schema_name") == 0 ||
232+
strcmp(def->defname, "table_name") == 0)
233+
{
234+
char *obj_name_opt = defGetString(def);
235+
236+
/*
237+
* PostgreSQL follows SQL syntax, so we do not allow empty
238+
* column_name, schema_name & table_name options.
239+
*/
240+
if (obj_name_opt && obj_name_opt[0] == '\0')
241+
ereport(ERROR,
242+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
243+
errmsg("value for option \"%s\" must not be empty string",
244+
def->defname)));
245+
}
230246
}
231247

232248
PG_RETURN_VOID();

contrib/postgres_fdw/sql/postgres_fdw.sql

+21
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ CREATE FOREIGN TABLE ft7 (
153153
c3 text
154154
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4');
155155

156+
CREATE FOREIGN TABLE ft8 (
157+
c1 int OPTIONS (column_name '') NOT NULL,
158+
c2 int NOT NULL,
159+
c3 text
160+
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4');
161+
162+
CREATE FOREIGN TABLE ft8 (
163+
c1 int NOT NULL,
164+
c2 int NOT NULL,
165+
c3 text
166+
) SERVER loopback3 OPTIONS (schema_name '', table_name 'T 4');
167+
168+
CREATE FOREIGN TABLE ft8 (
169+
c1 int NOT NULL,
170+
c2 int NOT NULL,
171+
c3 text
172+
) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name '');
173+
156174
-- ===================================================================
157175
-- tests for validator
158176
-- ===================================================================
@@ -213,10 +231,13 @@ ALTER USER MAPPING FOR public SERVER testserver1
213231
ALTER USER MAPPING FOR public SERVER testserver1
214232
OPTIONS (ADD sslkey 'value', ADD sslcert 'value');
215233

234+
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name '', table_name 'T 1');
235+
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name 'S 1', table_name '');
216236
ALTER FOREIGN TABLE ft1 OPTIONS (schema_name 'S 1', table_name 'T 1');
217237
ALTER FOREIGN TABLE ft2 OPTIONS (schema_name 'S 1', table_name 'T 1');
218238
ALTER FOREIGN TABLE ft1 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
219239
ALTER FOREIGN TABLE ft2 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
240+
ALTER FOREIGN TABLE ft2 ALTER COLUMN c2 OPTIONS (column_name '');
220241
\det+
221242

222243
-- Test that alteration of server options causes reconnection

0 commit comments

Comments
 (0)