Oracle 在 23ai 中采用了 BOOLEAN,PostgreSQL 则一直都有。
Oracle has adopted BOOLEAN in 23ai and PostgreSQL had it forever

原始链接: https://hexacluster.ai/blog/postgresql/oracles-adoption-of-native-boolean-data-type-vs-postgresql/

## Oracle 终于添加原生布尔支持 Oracle 最近的 23ai 版本引入了原生布尔数据类型,这一功能在 PostgreSQL 等其他数据库中早已可用(超过二十年)。 之前,Oracle 开发者通过使用 VARCHAR2 或 NUMBER 列来表示真/假值(例如 ‘Y’/’N’,1/0)来解决这一限制。 虽然这些解决方法可行,但由于冗余转换和更大的索引大小而影响了性能。 PostgreSQL 将布尔值高效地存储为单个字节,并允许直接进行逻辑运算,从而简化查询并加快过滤速度——尤其有利于分析型工作负载。 文章重点介绍了 Oracle(23ai 之前)和 PostgreSQL 之间的表结构以及插入/选择语句的差异,展示了原生布尔类型所实现的更简洁、更高效的逻辑。 最终,与模拟布尔值相比,使用 BOOLEAN 提供了更高的存储效率、类型安全性、查询简洁性和可读性。 HexaCluster 提供了一个迁移工具 HexaRocket,可以在数据库迁移期间自动将 Oracle 的 CHAR(1) 和 NUMBER(1) 列映射到 PostgreSQL 的 BOOLEAN 类型。

## Hacker News 上关于 Oracle 与 PostgreSQL 的讨论 一篇报道指出 Oracle 在其 23ai 数据库中采用了 BOOLEAN(PostgreSQL 早已拥有),由此在 Hacker News 上引发了一场讨论,并迅速演变成关于 Oracle 与 PostgreSQL 和 MySQL 等开源替代方案优缺点的更广泛辩论。 一些评论员批评了 Oracle 的命名规范,并认为原始帖子是数据库迁移服务的广告。讨论的核心集中在大型企业为何即使成本高昂仍坚持使用 Oracle,原因包括强大的支持、根深蒂固的惯性以及开源数据库难以复制的专业功能。 虽然许多人承认 PostgreSQL 的改进,但一些人认为它在某些方面仍然落后于 Oracle/MSSQL,尤其是在全局临时表和强大的行级别安全性等功能方面。另一些人则指出迁移风险、兼容性问题以及员工再培训成本等实际问题是切换的障碍。尽管对 Oracle 的公司文化和定价存在批评,但许多人认为该数据库本身是一个可靠的产品,将其比作企业领域的 Apple。
相关文章

原文

Oracle has finally introduced support for the Boolean data type in the release 23ai. Many thanks to the Engineers at Oracle for implementing this data type for an optimal performance. PL/SQL had BOOLEAN for decades, but developers were not able to declare native boolean type for columns of tables. For this reason, developers returned VARCHAR2/NUMBER from functions instead of BOOLEAN. Interestingly, PostgreSQL, an open-source relational database that has been widely used for many years and a migration target for Oracle, has had support for Boolean data for more than the past two decades. In this article, we will discuss about the workarounds used by developers before Oracle adopted boolean, and how it works in PostgreSQL.

The Hidden Cost of Simulating True and False

For database engineers and architects designing schemas, it required workarounds to represent "true" or "false" values as ‘Y’/’N’, ‘T’/’F’, or 1/0. While these may function adequately on the surface, they may hinder performance. The lack of a native Boolean data type can be a limitation in database design and impact storage efficiency.

The Workarounds We Lived With

Before Oracle’s recent version 23ai introduced BOOLEAN as the supported datatypes, following were some of the options.

file

Each of them may add redundant conversions and conditions in application code and PL/SQL functions, leading to more CPU work and larger indexes.

While Oracle took 2 decades to bring this up, PostgreSQL had it forever. Postgres stores Boolean values efficiently (internally as a single byte) and allows direct logical operations as follows.

SELECT * FROM employees WHERE is_active;
UPDATE orders SET is_verified = TRUE WHERE id = 1001;

 

What this clearly means is that it requires – No conversions, no string comparisons, just clean, logical semantics. This approach results in simpler queries, smaller indexes, and faster filtering, especially in analytical workloads with millions of rows.

Following chart demonstrates us the table structure in both Oracle and PostgreSQL, where we can avoid the additional checks in the case of PostgreSQL but not in Oracle.

Oracle Table Structure before 23ai Equivalent PostgreSQL Table Structure
CREATE TABLE user_flags (
   user_id     NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
   is_active   CHAR(1) CHECK (is_active IN ('Y', 'N')),
   is_verified NUMBER(1) CHECK (is_verified IN (0, 1))
);
        
CREATE TABLE user_flags (
   user_id     INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
   is_active   BOOLEAN,
   is_verified BOOLEAN
);
        


Insert Statements

You can directly insert TRUE and FALSE if boolean is supported natively, as seen in the case of PostgreSQL.

Oracle Compatible Insert before 23ai PostgreSQL Compatible Insert
INSERT INTO user_flags (is_active, is_verified)
VALUES ('Y', 1);

INSERT INTO user_flags (is_active, is_verified)
VALUES ('N', 0);
        
INSERT INTO user_flags (is_active, is_verified)
VALUES (TRUE, TRUE);

INSERT INTO user_flags (is_active, is_verified)
VALUES (FALSE, FALSE);
        


Select Statements

You can see simplified and performant select statements as seen in the table below.

Oracle PostgreSQL
SQL> SELECT * FROM genericmini_cdc.user_flags 
WHERE is_active = 'Y' AND is_verified = 1;

USER_ID | IS_ACTIVE | IS_VERIFIED
--------+-----------+------------
1       | Y         | 1
2       | Y         | 1
        
postgres=# SELECT * FROM user_flags 
WHERE is_active AND is_verified;

user_id | is_active | is_verified
--------+-----------+------------
1       | t         | t
(1 row)
        

In a nutshell, we can see boolean as the optimal data type over other relevant alternatives. Some of such benefits are listed in the table below.

Aspect BOOLEAN CHAR(1) / CHAR(5) SMALLINT / NUMBER(1)
Meaning Explicit logical type with TRUE, FALSE, and NULL Textual convention (e.g., 'Y', 'N', 'YES', 'NO') Numeric convention (1 = true, 0 = false)
Storage Typically 1 byte or bit 1–5 bytes depending on length and encoding 1–2 bytes
Type Safety Accepts only logical truth values May store invalid text (e.g., 'A', '?') May store non-logical numbers (e.g., 2, -1)
Query Simplicity Supports direct logical operations (AND, NOT, IS TRUE) Requires explicit comparison (='Y') Requires explicit comparison (=1)
Value Interpretation Automatically interprets TRUE, FALSE, T, F, YES, NO, 1, 0 (case-insensitive) Interpretation depends on convention and case sensitivity Limited to numeric values; cannot represent textual forms
Readability Self-explanatory and standardized Convention-dependent and less portable Convention-dependent and less intuitive
Language Integration Maps directly to native Boolean types Requires conversion logic Requires conversion logic

To simplify end-to-end database migrations from Oracle to PostgreSQL, we have announced our tool called Hexarocket. One of the advantages of using this tool is that – during data migration from Oracle to PostgreSQL, it can automatically map CHAR(1) and NUMBER(1) of Oracle to BOOLEAN in PostgreSQL.

Are you looking to migrate from Oracle to PostgreSQL or SQL Server to PostgreSQL? We are here to support with a simple and seamless migration experience within few clicks using HexaRocket. Request us for a demo on HexaRocket today: Schedule a demo.

Contact Us Today!

  • Pavan is a PostgreSQL Database Engineer and Developer at HexaCluster. With expertise in database migrations, performance tuning, and highly scalable PostgreSQL deployments, Pavan is considered one of the most loved PostgreSQL DBA and Developer by the Customers of HexaCluster. His expertise is not limited to PostgreSQL administration, development and migrations. Pavan is a seasoned developer who can build scalable applications using Golang, Java and Python languages.

联系我们 contact @ memedata.com