PostgreSQL UDF实现IF NOT EXISTS语法

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

标签

PostgreSQL , Greenplum , DDL , IF NOT EXISTS


背景

当对象存在时,不创建;当对象不存在时,创建。

在数据库中使用IF NOT EXISTS语法进行判断。

Syntax:  
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] table_name ( [  

有一些较老的版本,可能不支持IF NOT EXISTS语法,那么可以使用UDF实现类似的功能。

例如Greenplum:

create or replace function ddl_ine(sql text) returns int2 as $$  
declare  
begin  
  execute sql;   
  return 0;  -- 返回0表示正常  
  exception when duplicate_table then    
    raise notice '%', SQLERRM;   
    return 1;  -- 返回1表示已存在  
  when others then   
    raise notice '%ERROR: % %create table error:  %', chr(10), SQLERRM, chr(10), sql;   
    return 2;  -- 返回2表示DDL其他错误  
end;  
$$ language plpgsql strict;  

测试

postgres=# select ctbl('create table c(id int)');  
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.  
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.  
CONTEXT:  SQL statement "create table c(id int)"  
PL/pgSQL function "ctbl" line 3 at execute statement  
NOTICE:  relation "c" already exists  
 ctbl   
------  
    1  
(1 row)  
  
postgres=# select ctbl('create table e(id int)');  
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.  
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.  
CONTEXT:  SQL statement "create table e(id int)"  
PL/pgSQL function "ctbl" line 3 at execute statement  
 ctbl   
------  
    0  
(1 row)  
  
postgres=# select ctbl('create table e(id int9)');  
NOTICE:    
ERROR: type "int9" does not exist  
DETAIL:  create table error:  create table e(id int9)  
 ctbl   
------  
    2  
(1 row)  
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1月前
|
关系型数据库 MySQL
Mysql常用语法总结
Mysql常用语法总结
22 0
|
1月前
|
SQL 关系型数据库 MySQL
MySQL 数据库基本语法
SQL,全称Structured Query Language(结构化查询语言),是一种用于管理关系型数据库(RDBMS)的编程语言。SQL用于创建、修改、查询和删除数据库中的数据,以及定义数据库架构。它是数据库管理系统(DBMS)与应用程序之间的标准通信协议。
77 6
|
6月前
|
SQL 关系型数据库 MySQL
MySQL数据的基础语法
MySQL数据的基础语法
|
6月前
|
存储 关系型数据库 MySQL
【ChatGPT】输出MySQL常用语法汇总
【ChatGPT】输出MySQL常用语法汇总
|
6月前
|
关系型数据库 MySQL
【mysql】快速使用mysql exists 语法
【mysql】快速使用mysql exists 语法
47 1
|
2月前
|
关系型数据库 MySQL
mysql一些常用语法
mysql一些常用语法
|
2月前
|
存储 关系型数据库 MySQL
7、Mysql语法
7、Mysql语法
21 0
|
2月前
|
存储 关系型数据库 MySQL
小课堂 -- Mysql语法
小课堂 -- Mysql语法
16 0
|
2月前
|
SQL 存储 关系型数据库
mysql语法
mysql语法
28 2
|
2月前
|
SQL 关系型数据库 MySQL
like concat 兼容h2、mysql、pgsql语法
like concat 兼容h2、mysql、pgsql语法
34 0

相关产品

  • 云原生数据库 PolarDB