
# Foreign Scan
#### 算子说明
在GaussDB中，Foreign Scan是一种用于访问外部数据源的扫描器。它可以将外部数据源中的数据作为关系型数据库中的表来处理，从而实现对外部数据源的查询和操作。在GaussDB中，Foreign Scan可以通过扩展API来实现。用户可以编写自己的扩展程序，以实现对外部数据源的访问和操作。同时，GaussDB还提供了一些常用的扩展程序，如FDW（Foreign Data Wrapper），可以用于访问其他关系型数据库中的数据。
#### 典型场景
- 访问外部数据源：当需要访问外部数据源时，可以使用Foreign Scan来读取外部数据源中的数据。
- 数据集成：当需要将多个数据源中的数据进行集成时，可以使用Foreign Scan来将多个数据源中的数据作为关系型数据库中的表来处理。
- 数据迁移：当需要将数据从一个数据源迁移到另一个数据源时，可以使用Foreign Scan来读取源数据源中的数据，并将其插入到目标数据源中。
 
#### 示例
示例：访问外部数据源。
```
--数据准备。 
gaussdb=# SET enable_extension = true;
WARNING:  Extension is not a secure feature, and it may cause unexpected errors.
SET
gaussdb=# CREATE SERVER foo FOREIGN DATA WRAPPER file_fdw; 
CREATE SERVER
gaussdb=# CREATE FOREIGN TABLE ft (a int, b text) SERVER foo OPTIONS ("format" 'csv', "filename" '/gaussdb/username/data.csv', "null" 'NULL');
CREATE FOREIGN TABLE
--执行结果。 
gaussdb=# EXPLAIN SELECT * FROM ft; 
                         QUERY PLAN                         
------------------------------------------------------------
 Foreign Scan on ft  (cost=0.00..138.00 rows=1280 width=36)
   Foreign File: /gaussdb/username/data.csv
(2 rows)
--删除。
gaussdb=# DROP FOREIGN TABLE ft;
gaussdb=# DROP SERVER foo;
```
上述示例中，Foreign Scan算子输出信息如下所示。
| 信息名称         | 含义       |
|:---|:---|
| Foreign Scan | 算子的名称。   |
| Foreign File | 外部数据源文件。 |
   
示例2：数据集成
```
--数据准备。
gaussdb=# SET enable_extension = true;
WARNING:  Extension is not a secure feature, and it may cause unexpected errors.
SET
gaussdb=# CREATE SERVER f1 FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER
gaussdb=# CREATE FOREIGN TABLE ft1 (a int, b text) SERVER f1 OPTIONS ("format" 'csv', "filename" '/gaussdb/username/data.csv', "null" 'NULL');
CREATE FOREIGN TABLE
gaussdb=# CREATE SERVER f2 FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER
gaussdb=# CREATE FOREIGN TABLE ft2 (a int, b text) SERVER f2 OPTIONS ("format" 'csv', "filename" '/gaussdb/username/data.csv', "null" 'NULL');
CREATE FOREIGN TABLE
--执行结果。
gaussdb=# explain select ft.* from ft,ft2 where ft.a = ft2.a;
                               QUERY PLAN
-------------------------------------------------------------------------
 Hash Join  (cost=154.00..609.44 rows=16384 width=36)
   Hash Cond: (ft2.a = ft1.a)
   ->  Foreign Scan on ft2  (cost=0.00..266.00 rows=2560 width=4)
         Foreign File: /gaussdb/username/data.csv
   ->  Hash  (cost=138.00..138.00 rows=1280 width=36)
         ->  Foreign Scan on ft1  (cost=0.00..138.00 rows=1280 width=36)
               Foreign File: /gaussdb/username/data.csv
(7 rows)
--删除。
gaussdb=# DROP FOREIGN TABLE ft1;
gaussdb=# DROP FOREIGN TABLE ft2
gaussdb=# DROP SERVER f1;
gaussdb=# DROP SERVER f2;
```
示例3：数据迁移
```
--数据准备。
gaussdb=# SET enable_extension = true;
WARNING:  Extension is not a secure feature, and it may cause unexpected errors.
SET
gaussdb=# CREATE SERVER f1 FOREIGN DATA WRAPPER file_fdw;
CREATE SERVER
gaussdb=# CREATE FOREIGN TABLE ft1 (a int, b text) SERVER f1 OPTIONS ("format" 'csv', "filename" '/gaussdb/username/data.csv', "null" 'NULL');
CREATE FOREIGN TABLE
gaussdb=# copy ft1 from '/gaussdb/username/data.csv';
ERROR:  could not open file "/gaussdb" for reading: No such file or directory
gaussdb=# drop foreign table ft1;
DROP FOREIGN TABLE
gaussdb=# drop server f1;;
DROP SERVER
```
