官术网_书友最值得收藏!

  • Mastering PostgreSQL 12
  • Hans Jürgen Sch?nig
  • 242字
  • 2021-08-20 10:00:19

Improving ENUM handling

The possibility to create ENUM types (CREATE TYPE ... AS ENUM) has been around for quite some time now. In PostgreSQL 12, some improvements have been made to make sure that the type can be used even more efficiently. Let's create a simple type first and see how this works:

test=# CREATE TYPE currency AS ENUM ('USD', 'EUR', 'GBP');
CREATE TYPE

For the sake of simplicity, I have created a data type storing a couple of currencies. Mind that the order in the ENUM type does matter. If you order by a currency column, you will notice that the order returned by PostgreSQL is exactly as specified in the ENUM type. 

What has been added in PostgreSQL is the ability to modify an ENUM type inside a single transaction. The following example shows how that works:

test=# BEGIN;
BEGIN
test=# ALTER TYPE currency ADD VALUE 'CHF' AFTER 'EUR';
ALTER TYPE
test=# SELECT 'USD'::currency;
currency
----------
USD
(1 row)

However, there is one restriction: as you can see, the old ENUM values can be used inside the transaction directly. However, the new ones are not available within the same transaction:

test=# SELECT 'CHF'::currency;
ERROR: unsafe use of new value "CHF" of enum type currency
LINE 1: SELECT 'CHF'::currency;
^
HINT: New enum values must be committed before they can be used.
test=# COMMIT;
ROLLBACK

The transaction will error out if we want to access the new value inside the transaction.

主站蜘蛛池模板: 罗定市| 秀山| 富宁县| 淅川县| 论坛| 淅川县| 榕江县| 嘉义市| 广安市| 临漳县| 白河县| 蓬溪县| 普兰县| 永吉县| 杭锦旗| 虎林市| 南充市| 营口市| 伊宁市| 肥东县| 康定县| 巧家县| 黄骅市| 大埔区| 陕西省| 永康市| 宽城| 焉耆| 隆化县| 基隆市| 白玉县| 鄱阳县| 吐鲁番市| 九寨沟县| 南投县| 永泰县| 邮箱| 沂水县| 东莞市| 当雄县| 满洲里市|