-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpostgres_fdw_execute.patch
64 lines (61 loc) · 1.8 KB
/
postgres_fdw_execute.patch
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
diff --git a/contrib/postgres_fdw/postgres_fdw--1.0.sql b/contrib/postgres_fdw/postgres_fdw--1.0.sql
index a0f0fc1bf4..b81319be36 100644
--- a/contrib/postgres_fdw/postgres_fdw--1.0.sql
+++ b/contrib/postgres_fdw/postgres_fdw--1.0.sql
@@ -13,6 +13,11 @@ RETURNS void
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT;
+CREATE FUNCTION postgres_fdw_execute_custom_command(command text, server text)
+RETURNS void
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT;
+
CREATE FOREIGN DATA WRAPPER postgres_fdw
HANDLER postgres_fdw_handler
VALIDATOR postgres_fdw_validator;
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 856798ee72..1168544843 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -266,6 +266,7 @@ typedef struct
* SQL functions
*/
PG_FUNCTION_INFO_V1(postgres_fdw_handler);
+PG_FUNCTION_INFO_V1(postgres_fdw_execute_custom_command);
/*
* FDW callback routines
@@ -459,6 +460,35 @@ postgres_fdw_handler(PG_FUNCTION_ARGS)
}
/*
+ * Execute custom query on foreign server.
+ */
+Datum
+postgres_fdw_execute_custom_command(PG_FUNCTION_ARGS)
+{
+ const char *query = TextDatumGetCString(PG_GETARG_TEXT_P(0));
+ const char *server = TextDatumGetCString(PG_GETARG_TEXT_P(1));
+
+ Oid serverid;
+ UserMapping *user;
+ PGconn *connection;
+
+ PGresult *res;
+
+ /* Fetch user mapping */
+ serverid = get_foreign_server_oid(server, false);
+ user = GetUserMapping(GetUserId(), serverid);
+
+ connection = GetConnection(user, false);
+
+ res = pgfdw_exec_query(connection, query);
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ pgfdw_report_error(ERROR, res, connection, true, query);
+ PQclear(res);
+
+ PG_RETURN_VOID();
+}
+
+/*
* postgresGetForeignRelSize
* Estimate # of rows and width of the result of the scan
*