PostgreSQL Oracle 兼容性之 - add_months

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介:

有网友反映PostgreSQL oraface的add_months在某些日期与Oracle 的add_months不一致。
查了一下Oracle 的开发手册,add_months是这样定义的, 如果当前日期是月末,或者目标月没有当前日期的,取最后一天。
例子
2015年2月28日是2月的最后一天,所以按照Oracle的计算方法,无论加减多少个月结果应该都是目标月份的月末,而PostgreSQL 并不是这样的 :  

postgres=# select timestamp '2015-02-28' - interval '1 month';
      ?column?       
---------------------
 2015-01-28 00:00:00
(1 row)

postgres=# select oracle.add_months('2015-02-28 11:11:11+08',-1);
     add_months      
---------------------
 2015-01-28 11:11:11
(1 row)

以上查询在Oracle应该得到1月31号的结果。

目标月份没有当前日期,去目标月份的最后一天,比如3月30日减去一个月,不可能是2月30日,所以取2月的最后一天,这个规则是和Oracle一致的。

postgres=# select timestamp '2015-03-30' - interval '1 month';
      ?column?       
---------------------
 2015-02-28 00:00:00
(1 row)

Oracle add_months的解释如下 :
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions004.htm

ADD_MONTHS returns the date date plus integer months. The date argument can be a datetime value or any value that can be implicitly converted to DATE. The integer argument can be an integer or any value that can be implicitly converted to an integer. The return type is always DATE, regardless of the datatype of date. If date is the last day of the month or if the resulting month has fewer days than the day component of date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as date.

orafce中add_months的代码
SELECT ($1 + interval '1 month' * $2)::oracle.date;
问题就出在这里。
所以要和Oracle完全兼容,可以这样
创建两个这样的函数,如果当前日期是月末的话,则目标月取月末,否则就按照PG原来的算法。

create or replace function add_months(timestamp, int) returns timestamp as 
$$

declare
  i interval := ($2 || 'month');
  d1 date := date(to_timestamp($1::text,'yyyy-mm') + interval '1 month' - interval '1 day');
  d2 date := date($1);
  res timestamp;
begin
  select case when d1=d2 then ((to_char($1+i+interval '1 month', 'yyyy-mm')||'-01')::date - 1) + $1::time else $1+i end into res;
  return res;
end;

$$
 language plpgsql strict;

create or replace function add_months(timestamptz, int) returns timestamptz as 
$$

declare
  i interval := ($2 || 'month');
  d1 date := date(to_timestamp($1::text,'yyyy-mm') + interval '1 month' - interval '1 day');
  d2 date := date($1);
  res timestamptz;
begin
  select case when d1=d2 then ((to_char($1+i+interval '1 month', 'yyyy-mm')||'-01')::date - 1) + $1::timetz else $1+i end into res;
  return res;
end;

$$
 language plpgsql strict;

测试 : 
达到目的

postgres=# select add_months('2015-02-28 11:11:11+08',-1);
       add_months       
------------------------
 2015-01-31 11:11:11+08
(1 row)

postgres=# select add_months('2015-02-28 11:11:11+08',-12);
       add_months       
------------------------
 2014-02-28 11:11:11+08
(1 row)

postgres=# select add_months('2015-02-28 11:11:11+08',-24);
       add_months       
------------------------
 2013-02-28 11:11:11+08
(1 row)

postgres=# select add_months('2015-02-28 11:11:11+08',-36);
       add_months       
------------------------
 2012-02-29 11:11:11+08
(1 row)

postgres=# select add_months('2015-03-30 11:11:11+08',-1);
       add_months       
------------------------
 2015-02-28 11:11:11+08
(1 row)

postgres=# select add_months('2015-03-31 11:11:11+08',-1);
       add_months       
------------------------
 2015-02-28 11:11:11+08
(1 row)

postgres=# select add_months('2015-03-31 11:11:11+08',1);
       add_months       
------------------------
 2015-04-30 11:11:11+08
(1 row)

postgres=# select add_months('2015-03-30 11:11:11+08',1);
       add_months       
------------------------
 2015-04-30 11:11:11+08
(1 row)

postgres=# select add_months('2015-02-28 11:11:11+08',1);
       add_months       
------------------------
 2015-03-31 11:11:11+08
(1 row)
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
8月前
|
Oracle 关系型数据库 数据库
【赵渝强老师】在PostgreSQL中访问Oracle
本文介绍了如何在PostgreSQL中使用oracle_fdw扩展访问Oracle数据库数据。首先需从Oracle官网下载三个Instance Client安装包并解压,设置Oracle环境变量。接着从GitHub下载oracle_fdw扩展,配置pg_config环境变量后编译安装。之后启动PostgreSQL服务器,在数据库中创建oracle_fdw扩展及外部数据库服务,建立用户映射。最后通过创建外部表实现对Oracle数据的访问。文末附有具体操作步骤与示例代码。
466 6
【赵渝强老师】在PostgreSQL中访问Oracle
|
10月前
|
SQL Oracle 关系型数据库
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
我们经常发现,部分国产数据库声称与 Oracle兼容性高达90%,但在实际迁移过程中,仍需要频繁地修改业务应用的代码。为何实现与Oracle高兼容度的数据库产品如此困难?其中一个重要原因是Oracle兼容性不仅是模仿,而是一个非常复杂和工程量庞大的逆向工程。其技术实现的复杂性以及多如牛毛的细节,足以让多数“年轻”的数据库团队望洋兴叹。YashanDB作为一款从核心理论到关键系统均为原创的数据库产品,从构建初期就具备了技术优势,在Oracle兼容性实现上,敢于亮剑并充分发挥工匠精神,不断打磨,努力构筑一个真正形神兼备的数据库产品。以下将从YashanDB SQL引擎技术、Oracle兼容性的开发
|
SQL 存储 Oracle
【YashanDB观点】论Oracle兼容性,我们需要做什么
Oracle兼容性是目前国产数据库的关键任务之一,其直接影响到商业迁移的成本和竞争力。
251 8
|
Oracle NoSQL 关系型数据库
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
主流数据库对比:MySQL、PostgreSQL、Oracle和Redis的优缺点分析
2849 3
|
人工智能 Oracle 关系型数据库
一篇文章弄懂Oracle和PostgreSQL的Database Link
一篇文章弄懂Oracle和PostgreSQL的Database Link
|
SQL Oracle 关系型数据库
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
常用数据库的分页语句(mySQL、oracle、PostgreSQL、SQL Server)
|
存储 Oracle 关系型数据库
PolarDB 开源版通过orafce支持Oracle兼容性
背景PolarDB 的云原生存算分离架构, 具备低廉的数据存储、高效扩展弹性、高速多机并行计算能力、高速数据搜索和处理; PolarDB与计算算法结合, 将实现双剑合璧, 推动业务数据的价值产出, 将数据变成生产力.本文将介绍PolarDB开源版通过orafce支持Oracle兼容性 .测试环境为m...
372 0
|
7月前
|
存储 关系型数据库 测试技术
拯救海量数据:PostgreSQL分区表性能优化实战手册(附压测对比)
本文深入解析PostgreSQL分区表的核心原理与优化策略,涵盖性能痛点、实战案例及压测对比。首先阐述分区表作为继承表+路由规则的逻辑封装,分析分区裁剪失效、全局索引膨胀和VACUUM堆积三大性能杀手,并通过电商订单表崩溃事件说明旧分区维护的重要性。接着提出四维设计法优化分区策略,包括时间范围分区黄金法则与自动化维护体系。同时对比局部索引与全局索引性能,展示后者在特定场景下的优势。进一步探讨并行查询优化、冷热数据分层存储及故障复盘,解决分区锁竞争问题。
1005 2
|
关系型数据库 分布式数据库 PolarDB
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
582 0

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多