英文:
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
有两件事情让我想起:
-
创建一个在
ddl_command_end
上触发的 事件触发器,如果创建的表不正确,就抛出一个错误。 -
一个软性解决方案,不强制执行规则,但帮助合作的人:定义一个在
varchar(1000)
上的域string
,并在所有地方使用它。
英文:
There are two things that come to mind:
-
Create an event trigger that fires on
ddl_command_end
, and if the created table is not right, throw an error. -
The soft solution that does not enforce the rule, but helps people that cooperate: define a domain
string
overvarchar(1000)
and use that everywhere.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论