regclass 在 Postgresql 中代表什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13289107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:49:58  来源:igfitidea点击:

What does regclass signify in Postgresql

postgresqlcreate-table

提问by saji89

I have the following line in a CREATE TABLE statement:

我在 CREATE TABLE 语句中有以下行:

field1_id bigint DEFAULT nextval('table1_field1_id_seq'::regclass) NOT NULL,

What does regclass signify in the above? Is it an absolute requirement to add ::regclass?

上面的regclass是什么意思?添加是绝对要求::regclass吗?

N.B:I had seen the Postgresql documentation linkwhich tells about regclass, but couldn't understand it.

注意:我看过 Postgresql 文档链接,它讲述了regclass,但无法理解。

回答by Craig Ringer

No, you do not need the cast to regclasswhen calling a function like nextvalthat accepts a regclassparameter, as there is an implict cast from textto regclass. In some other contexts an explicit cast to regclassmay be required.

不,regclass在调用类似nextval接受regclass参数的函数时,您不需要转换为 to ,因为从textto存在隐式转换regclass。在其他一些上下文中,regclass可能需要显式强制转换为。

Explanation:

解释:

::regclassis a cast, like ::integer.

::regclass是一个演员表,比如::integer

regclassis a "magic" data type; it's actually an alias for oid, or "object identifier". See Object identifier typesin the documentation. Casting to regclassis a shortcut way of saying "this the name of a relation, please convert it to the oid of that relation". Casts to regclassare aware of the search_path, unlike querying pg_classfor a relation's oiddirectly, so casting to regclass isn't exactly equivalent to subquerying pg_class.

regclass是一种“神奇”的数据类型;它实际上是oid或“对象标识符”的别名。请参阅文档中的对象标识符类型。Casting toregclass是一种快捷方式,表示“这是一个关系的名称,请将其转换为该关系的 oid”。转换为regclass知道search_path,与直接查询pg_class关系不同oid,因此转换为 regclass 并不完全等同于 subquerying pg_class

Tables are relations. So are sequences, and views. So you can get the oid of a view or sequence by casting to regclass too.

表是关系。序列和视图也是如此。因此,您也可以通过强制转换为 regclass 来获得视图或序列的 oid。

There are implicit casts defined for textto regclass, so if you omit the explicit cast and you're calling a function that accepts regclassthe cast is done automatically. So you do notneed it in, for example, nextvalcalls.

textto定义了隐式转换regclass,因此如果您省略显式转换并且您正在调用接受转换regclass的函数,则会自动完成。这样你就不会需要它,例如,nextval电话。

There are other places where you may. For example you can't compare textdirectly with oid; so you can do this:

还有其他地方可以。例如,您不能text直接与oid;进行比较。所以你可以这样做:

regress=> select * from pg_class where oid = 'table1'::regclass;

but not this:

但不是这个:

regress=> select * from pg_class where oid = 'table1';
ERROR:  invalid input syntax for type oid: "table1"
LINE 1: select * from pg_class where oid = 'table1';


Just for fun I tried to write a query that performed the equivalent operation of casting to regclass. Don't use it, it's mostly for fun, and as an attempt to demo what's actually happening. Unless you're really interested in how Pg's guts work you can stop reading here.

只是为了好玩,我尝试编写一个查询,该查询执行与转换为regclass. 不要使用它,它主要是为了好玩,并试图演示实际发生的事情。除非你真的对 Pg 的胆量如何运作感兴趣,否则你可以停止阅读这里。

As I understand it, 'sequence_name'::regclass::oidis roughly equivalent to the following query:

据我了解,'sequence_name'::regclass::oid大致相当于以下查询:

WITH sp(sp_ord, sp_schema) AS (
  SELECT 
    generate_series(1, array_length(current_schemas('t'),1)),
    unnest(current_schemas('t'))
)
SELECT c.oid
FROM pg_class c INNER JOIN pg_namespace n ON (c.relnamespace = n.oid)
INNER JOIN sp ON (n.nspname = sp.sp_schema)
WHERE c.relname = 'sequence_name'
ORDER BY sp.sp_ord
LIMIT 1;

except that it's a lot shorter and a lot faster. See System information functionsfor the definition of current_schemas(...), etc.

除了它更短,速度更快。等的定义见系统信息函数current_schemas(...)

In other words:

换句话说:

  • Get a ab array listing all schemas we have access to and pair each entry up with an ordinal number for its position in the array
  • Search pg_classfor relations with matching names and associate each with its namespace (schema)
  • Sort the list of remaining relations by the order in which their schemas appeared in search_path
  • and pick the first match
  • 获取一个 ab 数组,列出我们可以访问的所有模式,并将每个条目与它在数组中的位置的序数配对
  • 搜索pg_class具有匹配名称的关系并将每个关系与其命名空间(架构)相关联
  • 按其模式出现的顺序对剩余关系列表进行排序 search_path
  • 并选择第一场比赛

回答by didierc

From what I understand of the documentation, oid are subdivided in types. regclassare database objects representing relations (so that they belong to the metadata table pg_class).

根据我对文档的理解,oid 被细分为类型。regclass是表示关系的数据库对象(因此它们属于元数据表 pg_class)。

It expresses a dependency between the sequence and the DEFAULTexpression (meaning the process of producing a default value if no explicit value is provided in a INSERT query for instance), so that if one emits a DROP SEQUENCE ...on the sequence, the query won't pass, unless it's cascaded (by writing DROP SEQUENCE table1_field1_id_seq CASCADE).

它表达了序列和DEFAULT表达式之间的依赖关系(例如,意味着如果在 INSERT 查询中没有提供显式值,则生成默认值的过程),因此如果DROP SEQUENCE ...在序列上发出 a ,查询将不会通过,除非它是级联的(通过写DROP SEQUENCE table1_field1_id_seq CASCADE)。