PostgreSQL 使用函数生成 外部表DDL

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

如果要生成大量的外部表, 写代码闲麻烦的话, PostgreSQL 9.5 可以通过import foreign schema 一键创建外部表, 以前的版本则可以通过如下方法快速的生成外部表的DDL.
创建postgresql外部表fdw

postgres=# create extension postgres_fdw;  
CREATE EXTENSION  

创建server, 指定远端数据库的ip, port, dbname

postgres=# create server fs foreign data wrapper postgres_fdw options (hostaddr '172.16.3.150', port '1921', dbname 'postgres');  
CREATE SERVER  

创建一个本地角色

postgres=# create role test login encrypted password 'test';  
CREATE ROLE  

配置本地角色teset使用server的user mapping, 指定连接到server的用户,密码.

postgres=# CREATE USER MAPPING FOR test SERVER fs OPTIONS (user 'postgres', password 'postgres');  
CREATE USER MAPPING  

将server的使用权给本地用户test

postgres=# grant usage on FOREIGN SERVER  fs to test;  
GRANT  

假设远端有一个表是orig

postgres=# \d orig  
     Table "public.orig"  
 Column |  Type   | Modifiers  
--------+---------+-----------  
 id     | integer |   
 x      | numeric |   

在test 用户下创建外部表

postgres=# \c postgres test  
You are now connected to database "postgres" as user "test".  
postgres=> create foreign table f_orig (id int, x numeric) server fs options(schema_name 'public', table_name 'orig');  
CREATE FOREIGN TABLE  

配置远端数据库的 pg_hba.conf, 允许访问.

postgres@db-172-16-3-150-> cd $PGDATA  
postgres@db-172-16-3-150-> vi pg_hba.conf  
host all all 0.0.0.0/0 md5  
"pg_hba.conf" 94L, 4495C written  
postgres@db-172-16-3-150-> pg_ctl reload  
server signaled  

测试外部表的访问

postgres=# \c postgres test  
You are now connected to database "postgres" as user "test".  
postgres=> select * from f_orig;  
  id  |   x      
------+--------  
    1 |  93.23  
    2 |  95.24  
    3 |  95.19  
.............  

为了快速刷新物化视图, 原表最好有UK或PK

postgres=> \c postgres postgres  
You are now connected to database "postgres" as user "postgres".  
postgres=# \d orig  
     Table "public.orig"  
 Column |  Type   | Modifiers   
--------+---------+-----------  
 id     | integer |   
 x      | numeric |   

postgres=# create unique index uk_orig on orig(id);  
CREATE INDEX  

使用如下函数自动生成外部表的DDL, 需要指定本地表的schema, tbl_name, 以及外部表需要创建在哪个schema下, 外部表的名称.

create or replace function create_ft(i_schema name, i_tbl name, i_ftschema text, i_ftname text) returns text as $$  
declare  
  v_attname name;  
  v_type text;  
  v_sql text;  
  v_nt text := '';  
  v_oid oid;  
begin  
  select oid into v_oid from pg_class where relnamespace=(select oid from pg_namespace where nspname=i_schema) and relname=i_tbl;  
  v_sql := 'create foreign table '||i_ftschema||'.'||i_ftname||' (';  
  for v_attname,v_type in SELECT a.attname,  
    pg_catalog.format_type(a.atttypid, a.atttypmod)  
    FROM pg_catalog.pg_attribute a  
    WHERE a.attrelid = v_oid AND a.attnum > 0 AND NOT a.attisdropped  
    ORDER BY a.attnum   
  LOOP  
    v_nt := v_nt||v_attname||' '||v_type||','||chr(10);  
  END LOOP;  
  select regexp_replace(v_nt,','||chr(10)||'$','') into v_nt;  
  v_sql := v_sql||v_nt||$_$) server fs options(schema_name '$_$||i_schema||$_$', table_name '$_$||i_tbl||$_$');$_$;  
  return v_sql;  
end;  
$$ language plpgsql strict;  

如下 :

postgres=# select create_ft('pg_catalog','pg_class','public','f_orig');  
                                       create_ft                                          
----------------------------------------------------------------------------------------  
 create foreign table public.f_orig (relname name,                                     +  
 relnamespace oid,                                                                     +  
 reltype oid,                                                                          +  
 reloftype oid,                                                                        +  
 relowner oid,                                                                         +  
 relam oid,                                                                            +  
 relfilenode oid,                                                                      +  
 reltablespace oid,                                                                    +  
 relpages integer,                                                                     +  
 reltuples real,                                                                       +  
 relallvisible integer,                                                                +  
 reltoastrelid oid,                                                                    +  
 relhasindex boolean,                                                                  +  
 relisshared boolean,                                                                  +  
 relpersistence "char",                                                                +  
 relkind "char",                                                                       +  
 relnatts smallint,                                                                    +  
 relchecks smallint,                                                                   +  
 relhasoids boolean,                                                                   +  
 relhaspkey boolean,                                                                   +  
 relhasrules boolean,                                                                  +  
 relhastriggers boolean,                                                               +  
 relhassubclass boolean,                                                               +  
 relispopulated boolean,                                                               +  
 relreplident "char",                                                                  +  
 relfrozenxid xid,                                                                     +  
 relminmxid xid,                                                                       +  
 relacl aclitem[],                                                                     +  
 reloptions text[]) server fs options(schema_name 'pg_catalog', table_name 'pg_class');  
(1 row)  

创建物化视图

postgres=# \c postgres test  
You are now connected to database "postgres" as user "test".  
postgres=> create materialized view mv1 as select * from f_orig;  
SELECT 1053  

快速刷新, 物化视图必须有UK或PK.

postgres=> create unique index uk_mv1 on mv1(id);  
CREATE INDEX  
postgres=> refresh materialized view concurrently mv1;  
REFRESH MATERIALIZED VIEW  

如果DDL不想输出换行符, 修改函数如下 :

create or replace function create_ft(i_schema name, i_tbl name, i_ftschema text, i_ftname text) returns text as $$  
declare  
  v_attname name;  
  v_type text;  
  v_sql text;  
  v_nt text := '';  
  v_oid oid;  
begin  
  select oid into v_oid from pg_class where relnamespace=(select oid from pg_namespace where nspname=i_schema) and relname=i_tbl;  
  v_sql := 'create foreign table '||i_ftschema||'.'||i_ftname||' (';  
  for v_attname,v_type in SELECT a.attname,  
    pg_catalog.format_type(a.atttypid, a.atttypmod)  
    FROM pg_catalog.pg_attribute a  
    WHERE a.attrelid = v_oid AND a.attnum > 0 AND NOT a.attisdropped  
    ORDER BY a.attnum   
  LOOP  
    v_nt := v_nt||v_attname||' '||v_type||',';  
  END LOOP;  
  select regexp_replace(v_nt,',$','') into v_nt;  
  v_sql := v_sql||v_nt||$_$) server fs options(schema_name '$_$||i_schema||$_$', table_name '$_$||i_tbl||$_$');$_$;  
  return v_sql;  
end;  
$$ language plpgsql strict;  

批量输出DDL

postgres=# copy (select create_ft('public',tablename,'public','ft_'||tablename) from pg_tables where schemaname='public') to '/home/postgres/p';  
COPY 14  
postgres=> \!  
[postgres@db-172-16-3-150 ~]$ cat p  
create foreign table public.ft_p4 (ov integer,v_id integer,r_chkv numeric,p_yv numeric,r_xv numeric,dev numeric,v_slope numeric,v_inter numeric,v_r2 numeric,sampcnt integer) server fs options(schema_name 'public', table_name 'p4');  
create foreign table public.ft_tmp1 (id integer,x numeric,y numeric) server fs options(schema_name 'public', table_name 'tmp1');  
create foreign table public.ft_tmp2 (id integer,x numeric,y numeric) server fs options(schema_name 'public', table_name 'tmp2');  
create foreign table public.ft_tmp3 (id integer,x numeric,y numeric) server fs options(schema_name 'public', table_name 'tmp3');  
create foreign table public.ft_tmp4 (id integer,x numeric,y numeric) server fs options(schema_name 'public', table_name 'tmp4');  
create foreign table public.ft_p2 (ov integer,v_id integer,r_chkv numeric,p_yv numeric,r_xv numeric,dev numeric,v_slope numeric,v_inter numeric,v_r2 numeric,sampcnt integer) server fs options(schema_name 'public', table_name 'p2');  
create foreign table public.ft_zjdr (id numeric) server fs options(schema_name 'public', table_name 'zjdr');  
create foreign table public.ft_zj (id integer,cnt numeric) server fs options(schema_name 'public', table_name 'zj');  
create foreign table public.ft_t1 (c1 text,c2 numeric,c3 numeric,c4 numeric,c5 numeric,c6 numeric,c7 numeric) server fs options(schema_name 'public', table_name 't1');  
create foreign table public.ft_test (id integer,cnt numeric) server fs options(schema_name 'public', table_name 'test');  
create foreign table public.ft_orig (id integer,x numeric) server fs options(schema_name 'public', table_name 'orig');  
create foreign table public.ft_tmp (id integer,x numeric,y numeric) server fs options(schema_name 'public', table_name 'tmp');  
create foreign table public.ft_p3 (ov integer,v_id integer,r_chkv numeric,p_yv numeric,r_xv numeric,dev numeric,v_slope numeric,v_inter numeric,v_r2 numeric,sampcnt integer) server fs options(schema_name 'public', table_name 'p3');  
create foreign table public.ft_p1 (ov integer,v_id integer,r_chkv numeric,p_yv numeric,r_xv numeric,dev numeric,v_slope numeric,v_inter numeric,v_r2 numeric,sampcnt integer) server fs options(schema_name 'public', table_name 'p1');  

这样生成的代码比较紧凑, 直接执行以上生成的SQL就可以创建这些外部表了.
虽然没有import foreign schema语法简便, 但是相比手工写DDL已经好很多了.

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
7月前
|
SQL 关系型数据库 测试技术
PolarDB的Online DDL功能验证实验
本场景带您体验如何在PolarDB-X中进行Online DDL。
955 0
|
7月前
|
SQL 关系型数据库 分布式数据库
PolarDB在尝试同步DDL任务时出现了问题
PolarDB在尝试同步DDL任务时出现了问题
72 1
|
移动开发 关系型数据库 PostgreSQL
PostgreSQL 条件判断函数
PostgreSQL 条件判断函数
752 1
|
关系型数据库 PostgreSQL
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
PostgreSQL 计算字符串字符数函数(CHAR_LENGTH(str))和字符串长度函数(LENGTH(str))
1217 0
|
2月前
|
SQL 存储 缓存
PostgreSQL函数管理接口
学习PostgreSQL服务端开发必须要对函数管理接口有比较深入的了解
143 0
|
1月前
|
关系型数据库 PostgreSQL
postgresql日程排程函数的编写实例
postgresql日程排程函数的编写实例
|
2月前
|
SQL 关系型数据库 分布式数据库
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
在PolarDB for PostgreSQL中,你可以使用LIKE运算符来实现类似的查询功能,而不是使用IF函数
43 7
|
4月前
|
SQL 关系型数据库 C语言
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
PostgreSQL【应用 03】Docker部署的PostgreSQL扩展SQL之C语言函数(编写、编译、载入)计算向量余弦距离实例分享
45 0
|
4月前
|
SQL 关系型数据库 数据库
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
PostgreSQL【应用 02】扩展SQL之C语言函数(编写、编译、载入)实例分享
49 0
|
4月前
|
SQL 关系型数据库 PostgreSQL
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
PostgreSQL【SQL 01】根据条件更新字段值或追加信息STRPOS(string, substring)函数使用及LIKE函数对比
56 0

相关产品

  • 云原生数据库 PolarDB