Move foreign connection caching module into contrib/postgresql_fdw.
authorShigeru Hanada <[email protected]>
Wed, 13 Oct 2010 08:18:56 +0000 (17:18 +0900)
committerShigeru Hanada <[email protected]>
Wed, 13 Oct 2010 08:18:56 +0000 (17:18 +0900)
15 files changed:
contrib/postgresql_fdw/Makefile
contrib/postgresql_fdw/fsconnection.c [moved from src/backend/foreign/fsconnection.c with 99% similarity]
contrib/postgresql_fdw/fsconnection.h [new file with mode: 0644]
contrib/postgresql_fdw/postgresql_fdw.c
contrib/postgresql_fdw/postgresql_fdw.sql.in
contrib/postgresql_fdw/uninstall_postgresql_fdw.sql
doc/src/sgml/catalogs.sgml
src/backend/catalog/system_views.sql
src/backend/commands/discard.c
src/backend/executor/nodeForeignscan.c
src/backend/foreign/Makefile
src/backend/utils/init/miscinit.c
src/include/catalog/pg_proc.h
src/include/foreign/foreign.h
src/test/regress/expected/rules.out

index c20cf80f9ff3490400de8b19cc6a233b2f30d77c..2dd917be38da97b7a1d821e579a6c93264d07aa6 100644 (file)
@@ -2,7 +2,7 @@
 
 MODULE_big = postgresql_fdw
 PG_CPPFLAGS = -I$(libpq_srcdir)
-OBJS   = postgresql_fdw.o
+OBJS   = postgresql_fdw.o fsconnection.o
 SHLIB_LINK = $(libpq)
 
 DATA_built = postgresql_fdw.sql 
similarity index 99%
rename from src/backend/foreign/fsconnection.c
rename to contrib/postgresql_fdw/fsconnection.c
index 1e351a7766e221a5bdb411a5e4849d53f8c7faee..fda112ecd82155c2436303c4f7e174d637cefde2 100644 (file)
@@ -20,6 +20,7 @@
 #include "utils/builtins.h"
 #include "utils/catcache.h"
 
+#include "fsconnection.h"
 
 extern Datum pg_foreign_connections(PG_FUNCTION_ARGS);
 
diff --git a/contrib/postgresql_fdw/fsconnection.h b/contrib/postgresql_fdw/fsconnection.h
new file mode 100644 (file)
index 0000000..e1c886d
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * fsconnection.h
+ *
+ * Cache connections to foreign PostgreSQL servers.
+ *
+ * contrib/postgresql_fdw/postgresql_fdw.h
+ * Copyright (c) 2010, PostgreSQL Global Development Group
+ * ALL RIGHTS RESERVED;
+ *
+ */
+
+#ifndef FSCONNECTION_H
+#define FSCONNECTION_H
+
+/* foreign server connections */
+extern FSConnection *ConnectToForeignServer(FdwRoutine *routine,
+           ForeignServer *server, UserMapping *user, const char *conname);
+extern void RegisterFSConnection(FSConnection *conn, FdwRoutine *routine,
+                                const char *conname);
+extern FSConnection *GetFSConnectionByName(const char *conname, FdwRoutine **routine);
+extern bool DisconnectForeignServer(const char *conname);
+extern void DisconnectAllForeignServers(void);
+
+#endif   /* FSCONNECTION_H */
index 7fa89554427895cf1f8c2b1e98dce60c54833c14..cf640ecee0bcb8c8f0f2afafb0a28d53298b9df1 100644 (file)
@@ -31,6 +31,7 @@
 #include "utils/syscache.h"
 
 #include "postgresql_fdw.h"
+#include "fsconnection.h"
 
 PG_MODULE_MAGIC;
 
index 1f784e110d2ec0c327b7035b8be8270820d15758..d2ed838f6a661ab6e7ff3b0ed7a430c861385321 100644 (file)
@@ -8,3 +8,19 @@ RETURNS fdw_handler
 AS 'MODULE_PATHNAME','postgresql_fdw_handler'
 LANGUAGE C STRICT;
 
+CREATE OR REPLACE FUNCTION pg_foreign_connections(conname OUT text, serverid OUT oid, userid OUT oid)
+    RETURNS SETOF record
+    AS 'MODULE_PATHNAME','pg_foreign_connections'
+    LANGUAGE C STRICT;
+
+CREATE OR REPLACE VIEW pg_foreign_connections AS
+    SELECT c.conname as conname,
+           s.srvname as srvname,
+           a.rolname as usename
+      FROM pg_foreign_connections() c
+           JOIN pg_foreign_server s ON (s.oid = c.serverid)
+           JOIN pg_authid a ON (a.oid = c.userid);
+
+GRANT EXECUTE ON FUNCTION pg_foreign_connections(conname OUT text, serverid OUT oid, userid OUT oid) TO public;
+GRANT SELECT ON pg_foreign_connections TO public;
+
index 7052d8d47743539d3d02dbc15d3e84ec7b608ae7..037551ec7540b5e2b5c00b6598dc744198da1a12 100644 (file)
@@ -4,4 +4,6 @@
 set search_path = public;
 
 DROP FUNCTION postgresql_fdw_handler ();
+DROP VIEW pg_foreign_connections;
+DROP FUNCTION pg_foreign_connections(conname OUT text, serverid OUT oid, userid OUT oid);
 
index 54b54c4ccf836449e851ba4811d72bd2a23e34f2..f41d0f2594b43f22ec398fc022ac540adde5777f 100644 (file)
       <entry>open cursors</entry>
      </row>
 
-     <row>
-      <entry><link linkend="view-pg-foreign-connections"><structname>pg_foreign_connections</structname></link></entry>
-      <entry>established foreign connections</entry>
-     </row>
-
      <row>
       <entry><link linkend="view-pg-group"><structname>pg_group</structname></link></entry>
       <entry>groups of database users</entry>
 
  </sect1>
 
- <sect1 id="view-pg-foreign-connections">
-  <title><structname>pg_foreign_connections</structname></title>
-
-  <indexterm zone="view-pg-foreign-connections">
-   <primary>pg_foreign_connections</primary>
-  </indexterm>
-
-  <para>
-   The <structname>pg_foreign_connections</structname> view lists
-   the foreign connections that are currently available.
-   Foreign connections are established in several ways:
-   <itemizedlist>
-    <listitem>
-     <para>
-      via the query for a foreign table
-     </para>
-    </listitem>
-
-    <listitem>
-     <para>
-      via the use of dblink functions, see <xref linkend="dblink">
-     </para>
-    </listitem>
-   </itemizedlist>
-
-   The <structname>pg_foreign_connections</structname> view displays
-   foreign connections created by any of these means.
-   Foreign connections only exist for the duration
-   of the backend lifetime which established the connection,
-   and cannot be used from other backends.
-   Use <command>DISCARD ALL</command> command to close all foreign
-   connections.
-  </para>
-
-  <table>
-   <title><structname>pg_foreign_connections</> Columns</title>
-
-   <tgroup cols="3">
-    <thead>
-     <row>
-      <entry>Name</entry>
-      <entry>Type</entry>
-      <entry>Description</entry>
-     </row>
-    </thead>
-
-    <tbody>
-     <row>
-      <entry><structfield>conname</structfield></entry>
-      <entry><type>text</type></entry>
-      <entry>The name of the connection</entry>
-     </row>
-
-     <row>
-      <entry><structfield>srvname</structfield></entry>
-      <entry><type>name</type></entry>
-      <entry>The name of the foreign server which was used to
-       establish the connection</entry>
-     </row>
-
-     <row>
-      <entry><structfield>usename</structfield></entry>
-      <entry><type>name</type></entry>
-      <entry>The name of the local user which was used to
-       establish the connection</entry>
-     </row>
-
-     <row>
-      <entry><structfield>fdwname</structfield></entry>
-      <entry><type>name</type></entry>
-      <entry>The name of the foreign-data wrapper which was
-      used to establish the connection</entry>
-     </row>
-    </tbody>
-   </tgroup>
-  </table>
-
-  <para>
-   The <structname>pg_cursors</structname> view is read only.
-  </para>
-
- </sect1>
-
  <sect1 id="view-pg-group">
   <title><structname>pg_group</structname></title>
 
index f0366b6c9c16036a256ac4f780017279b89c235c..09574c3e82cf116294346cd8579d103990550d0f 100644 (file)
@@ -288,16 +288,6 @@ CREATE VIEW pg_timezone_abbrevs AS
 CREATE VIEW pg_timezone_names AS
     SELECT * FROM pg_timezone_names();
 
-CREATE VIEW pg_foreign_connections AS
-    SELECT C.connection_name AS conname,
-           S.srvname AS srvname,
-           U.rolname AS usename,
-           D.fdwname AS fdwname
-    FROM pg_foreign_connections() AS C
-         LEFT JOIN pg_authid U ON C.userid = U.oid
-         LEFT JOIN pg_foreign_server S ON C.serverid = S.oid
-         LEFT JOIN pg_foreign_data_wrapper D ON S.srvfdw = D.oid;
-
 -- Statistics views
 
 CREATE VIEW pg_stat_all_tables AS 
index 33130b4502747b94febf3c2ff70f26f600d9ee81..de8d75bce2e1bba08256318cd90c99ef2d0456fa 100644 (file)
@@ -69,5 +69,4 @@ DiscardAll(bool isTopLevel)
    LockReleaseAll(USER_LOCKMETHOD, true);
    ResetPlanCache();
    ResetTempTableNamespace();
-   DisconnectAllForeignServers();
 }
index 7bb47884af34ab8379c04b3f779e7c7697dbd7f7..f8c305a6ae93476d57131426212c87b8ef3c0c35 100644 (file)
@@ -172,10 +172,8 @@ ExecInitForeignScan(ForeignScan *node, EState *estate, int eflags)
    scanstate->routine = GetFdwRoutine(scanstate->wrapper->fdwhandler);
 
    /* connect to the foreign server and prepare to execute scan */
-   scanstate->conn = ConnectToForeignServer(scanstate->routine,
-                                            scanstate->server,
-                                            scanstate->user,
-                                            scanstate->server->servername);
+   scanstate->conn = scanstate->routine->ConnectServer(scanstate->server,
+                                                       scanstate->user);
    scanstate->routine->Open(scanstate);
 
    return scanstate;
index 1367885689d64b20ce4ec84fd08b1ef5b5edad93..6b7fa8f57bfaa41857df5e986909f1fef8058e20 100644 (file)
@@ -12,6 +12,6 @@ subdir = src/backend/foreign
 top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
-OBJS= foreign.o fsconnection.o csv_fdw.o
+OBJS= foreign.o csv_fdw.o
 
 include $(top_srcdir)/src/backend/common.mk
index 87a424f4760063ebf5dc301bbeb6e6a51da65cf5..380162bace88f028bd5e5441f743df703e52f316 100644 (file)
@@ -262,10 +262,6 @@ SetOuterUserId(Oid userid)
    AssertArg(OidIsValid(userid));
    OuterUserId = userid;
 
-   /* Discard all foreign server connections */
-   if (CurrentUserId != userid)
-       DisconnectAllForeignServers();
-
    /* We force the effective user ID to match, too */
    CurrentUserId = userid;
 }
@@ -291,10 +287,6 @@ SetSessionUserId(Oid userid, bool is_superuser)
    SessionUserIsSuperuser = is_superuser;
    SetRoleIsActive = false;
 
-   /* Discard all foreign server connections */
-   if (CurrentUserId != userid)
-       DisconnectAllForeignServers();
-
    /* We force the effective user IDs to match, too */
    OuterUserId = userid;
    CurrentUserId = userid;
index 295901db383310b584c9dbfc145782d053f12dbb..1134e64f1acbefe632ed8a3b012cbab8ac8acac7 100644 (file)
@@ -4201,8 +4201,6 @@ DATA(insert OID = 3035 (  pg_listening_channels PGNSP PGUID 12 1 10 0 f f f t t
 DESCR("get the channels that the current backend listens to");
 DATA(insert OID = 3036 (  pg_notify                PGNSP PGUID 12 1 0 0 f f f f f v 2 0 2278 "25 25" _null_ _null_ _null_ _null_ pg_notify _null_ _null_ _null_ ));
 DESCR("send a notification event");
-DATA(insert OID = 3539 (  pg_foreign_connections PGNSP PGUID 12 1 1000 0 f f f t t s 0 0 2249 "" "{25,26,26}" "{o,o,o}" "{connection_name,serverid,userid}" _null_ pg_foreign_connections _null_ _null_ _null_ ));
-DESCR("get the foreign server connections for this session");
 
 /* non-persistent series generator */
 DATA(insert OID = 1066 (  generate_series PGNSP PGUID 12 1 1000 0 f f f t t i 3 0 23 "23 23 23" _null_ _null_ _null_ _null_ generate_series_step_int4 _null_ _null_ _null_ ));
index ec111ad2f5b0fbf86ab1732cf5c562238555ddf3..81da822b9332ac2d5a4e1abee4fb6c8142e640bf 100644 (file)
@@ -92,13 +92,4 @@ extern int flatten_generic_options(List *options,
 extern void ATExecGenericOptions(Relation rel, List *options);
 extern bool is_libpq_connection_option(const char *option);
 
-/* foreign server connections */
-extern FSConnection *ConnectToForeignServer(FdwRoutine *routine,
-           ForeignServer *server, UserMapping *user, const char *conname);
-extern void RegisterFSConnection(FSConnection *conn, FdwRoutine *routine,
-                                const char *conname);
-extern FSConnection *GetFSConnectionByName(const char *conname, FdwRoutine **routine);
-extern bool DisconnectForeignServer(const char *conname);
-extern void DisconnectAllForeignServers(void);
-
 #endif   /* FOREIGN_H */
index 314f4cbf2a1ceb332d647f41c3c14d784d26b76a..c7a796a2a54c23814a6189b32ac19805b6f11ebb 100644 (file)
@@ -1280,7 +1280,6 @@ SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schem
 -----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  iexit                       | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
  pg_cursors                  | SELECT c.name, c.statement, c.is_holdable, c.is_binary, c.is_scrollable, c.creation_time FROM pg_cursor() c(name, statement, is_holdable, is_binary, is_scrollable, creation_time);
- pg_foreign_connections      | SELECT c.connection_name AS conname, s.srvname, u.rolname AS usename, d.fdwname FROM (((pg_foreign_connections() c(connection_name, serverid, userid) LEFT JOIN pg_authid u ON ((c.userid = u.oid))) LEFT JOIN pg_foreign_server s ON ((c.serverid = s.oid))) LEFT JOIN pg_foreign_data_wrapper d ON ((s.srvfdw = d.oid)));
  pg_group                    | SELECT pg_authid.rolname AS groname, pg_authid.oid AS grosysid, ARRAY(SELECT pg_auth_members.member FROM pg_auth_members WHERE (pg_auth_members.roleid = pg_authid.oid)) AS grolist FROM pg_authid WHERE (NOT pg_authid.rolcanlogin);
  pg_indexes                  | SELECT n.nspname AS schemaname, c.relname AS tablename, i.relname AS indexname, t.spcname AS tablespace, pg_get_indexdef(i.oid) AS indexdef FROM ((((pg_index x JOIN pg_class c ON ((c.oid = x.indrelid))) JOIN pg_class i ON ((i.oid = x.indexrelid))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) LEFT JOIN pg_tablespace t ON ((t.oid = i.reltablespace))) WHERE ((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char"));
  pg_locks                    | SELECT l.locktype, l.database, l.relation, l.page, l.tuple, l.virtualxid, l.transactionid, l.classid, l.objid, l.objsubid, l.virtualtransaction, l.pid, l.mode, l.granted FROM pg_lock_status() l(locktype, database, relation, page, tuple, virtualxid, transactionid, classid, objid, objsubid, virtualtransaction, pid, mode, granted);
@@ -1335,7 +1334,7 @@ SELECT viewname, definition FROM pg_views WHERE schemaname <> 'information_schem
  shoelace_obsolete           | SELECT shoelace.sl_name, shoelace.sl_avail, shoelace.sl_color, shoelace.sl_len, shoelace.sl_unit, shoelace.sl_len_cm FROM shoelace WHERE (NOT (EXISTS (SELECT shoe.shoename FROM shoe WHERE (shoe.slcolor = shoelace.sl_color))));
  street                      | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
  toyemp                      | SELECT emp.name, emp.age, emp.location, (12 * emp.salary) AS annualsal FROM emp;
-(57 rows)
+(56 rows)
 
 SELECT tablename, rulename, definition FROM pg_rules 
    ORDER BY tablename, rulename;