PostgreSQL中的DO- 有条件的创建函数

DO [ LANGUAGE lang_name ] code
 

DO执行一段匿名代码块, 换句话说,在程序语言过程中一次性执行的匿名函数。

代码块被看做是没有参数的一段函数体,返回值类型是void。它的解析和执行时同一时刻发生的。

可选属性LANGUAGE可以在代码块之前写,也可以写在代码块的后面。 


code

程序语言代码可以被执行的。程序语言必须指定为字符串才行, 就像命令CREATE FUNCTION,推荐使用美元符号一样。

lang_name

用来解析代码的程序语言的名字,如果缺省,默认的语言是plpgsql


应用场景:

比如在数据库满足一定条件下,比如,必须是UTF-8编码时,才创建某个函数,则可以将函数包裹在code中。

例,

DO $DO$ 
DECLARE coding varchar;
BEGIN
SELECT getdatabaseencoding() INTO coding;
IF coding = 'UTF8' THEN
EXECUTE 'CREATE OR REPLACE FUNCTION pg_catalog.lengthb(character) RETURNS integer AS $$
    SELECT CASE octet_length($1)
     WHEN 0 THEN NULL
    ELSE octet_length($1) END;
$$ LANGUAGE SQL IMMUTABLE STRICT;
COMMENT ON FUNCTION pg_catalog.lengthb(character) IS ''Returns the length of the specified string, using bytes instead of characters'';'; 
END IF;
END$DO$;


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This release contains a variety of fixes from 14.0. For information about new features in major release 14, see Section E.2. A dump/restore is not required for those running 14.X. However, note that installations using physical replication should update standby servers before the primary server, as explained in the third changelog entry below. Also, several bugs have been found that may have resulted in corrupted indexes, as explained in the next several changelog entries. If any of those cases apply to you, it's recommended to reindex possibly-affected indexes after updating. Make the server reject extraneous data after an SSL or GSS encryption handshake (Tom Lane) A man-in-the-middle with the ability to inject data into the TCP connection could stuff some cleartext data into the start of a supposedly encryption-protected database session. This could be abused to send faked SQL commands to the server, although that would only work if the server did not demand any authentication data. (However, a server relying on SSL certificate authentication might well not do so.) The PostgreSQL Project thanks Jacob Champion for reporting this problem. (CVE-2021-23214) Make libpq reject extraneous data after an SSL or GSS encryption handshake (Tom Lane) A man-in-the-middle with the ability to inject data into the TCP connection could stuff some cleartext data into the start of a supposedly encryption-protected database session. This could probably be abused to inject faked responses to the client's first few queries, although other details of libpq's behavior make that harder than it sounds. A different line of attack is to exfiltrate the client's password, or other sensitive data that might be sent early in the session. That has been shown to be possible with a server vulnerable to CVE-2021-23214. The PostgreSQL Project thanks Jacob Champion for reporting this problem. (CVE-2021-23222) Fix physical replication for cases where the primary crashes after shipping a WAL se

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值