0001-Prevent-self-referencing-foreign-table-on-loopback.patch
text/x-patch
Filename: 0001-Prevent-self-referencing-foreign-table-on-loopback.patch
Type: text/x-patch
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
Series: patch 0001
| File | + | − |
|---|---|---|
| src/backend/commands/foreigncmds.c | 36 | 0 |
diff --git a/src/backend/commands/foreigncmds.c b/src/backend/commands/foreigncmds.c
index c4852be2eb2..5b48932d3f6 100644
--- a/src/backend/commands/foreigncmds.c
+++ b/src/backend/commands/foreigncmds.c
@@ -1545,6 +1545,42 @@ CreateForeignTable(CreateForeignTableStmt *stmt, Oid relid)
fdw = GetForeignDataWrapper(server->fdwid);
+ if (stmt->options != NIL) {
+ char *table_name = NULL;
+ ListCell *lc;
+
+ foreach(lc, stmt->options) {
+ DefElem *def = (DefElem *) lfirst(lc);
+ if (strcmp(def->defname, "table_name") == 0) {
+ table_name = defGetString(def);
+ break;
+ }
+ }
+
+ if (table_name && strcmp(stmt->base.relation->relname, table_name) == 0) {
+ /* Check if the server is pointing to the same database */
+ char *server_dbname = NULL;
+
+ foreach(lc, server->options) {
+ DefElem *def = (DefElem *) lfirst(lc);
+ if (strcmp(def->defname, "dbname") == 0) {
+ server_dbname = defGetString(def);
+ break;
+ }
+ }
+
+ if (server_dbname == NULL ||
+ strcmp(server_dbname, get_database_name(MyDatabaseId)) == 0) {
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+ errmsg("foreign table \"%s\" cannot reference itself",
+ stmt->base.relation->relname),
+ errhint("Foreign table pointing to the same table on the same database "
+ "creates circular reference")));
+ }
+ }
+ }
+
/*
* Insert tuple into pg_foreign_table.
*/