在Postgres中设置全局列长度限制。

huangapple go评论63阅读模式
英文:

Setting a global limit on column length in Postgres

问题

我对Postgres相当陌生(有Oracle背景)。我希望阻止人们尝试将JSON、XML、二进制文件等存储在Postgres的单个列中(这是为了避免TOAST)。

问题在于过去,开发人员曾试图将数亿行XML存储在我们的数据库中,我们只在问题发生后才发现。所有内容最终都会传送到TOAST(或在Oracle中的Lob对象),性能受到影响,存储占用也受到影响。

我想在列的长度上设定一个绝对限制。
例如,不允许TEXT或VARCHAR,只允许CHARACTER VARYING(n)、VARCHAR(n),其中“n”最多为500个字符。

在Oracle中,我只需在创建表的全局触发器上创建触发器,其中模式不在SYS(或默认/系统模式)中,然后在CLOB、BLOB、XML类型上返回异常。

在Postgres中是否有这样做的方法?

我假设如果Postgres数据库叫做foo,那么我可以在该数据库的表上创建触发器。

在过去,我们曾测试过一些用于Postgres的JSON存储插件,但它们的功能不足够。实际上,如果使用关系型数据库管理系统存储数据,开发人员需要拥抱关系模型。

英文:

I'm quite new to Postgres (Oracle Background). I wish to prevent people attempting to store JSON, XML, Binary files, etc in a single column in Postgres (This is to avoid TOAST).

The issue is that in the past, developers have attempted to store 100's of millions of lines of XML in our Databases and we've only caught the issue after it's happened. Everything ends up going out to TOAST (Or Lob Objects in the case of Oracle) and performance suffers, along with the storage footprint.

I want to set an absolute limit on the length a column can be.
So for example no TEXT or VARCHAR, and allowing CHARACTER VARYING(n), VARCHAR(n) where "n" is a max of say 500 characters.

In Oracle I'd just create global trigger on create table where schema not in SYS (or default/system schemas) and return and exception on CLOB, BLOB, XML type.

Is there a way to do this in Postgres?

I'm assuming that if the postgres database was called foo that I could just create a trigger on tables in that DB

In the past we have tested some JSON storage add-ons for postgres but they do not do enough. Realistically developers need to embrace the relational model if using a RDBMS to store data.

答案1

得分: 1

有两件事情让我想起:

  1. 创建一个在 ddl_​command_​end 上触发的 事件触发器,如果创建的表不正确,就抛出一个错误。

  2. 一个软性解决方案,不强制执行规则,但帮助合作的人:定义一个在 varchar(1000) 上的域 string,并在所有地方使用它。

英文:

There are two things that come to mind:

  1. Create an event trigger that fires on ddl_​command_​end, and if the created table is not right, throw an error.

  2. The soft solution that does not enforce the rule, but helps people that cooperate: define a domain string over varchar(1000) and use that everywhere.

huangapple
  • 本文由 发表于 2023年6月29日 22:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定