regclass在Postgresql中表示什么 [英] What does regclass signify in Postgresql

查看:1059
本文介绍了regclass在Postgresql中表示什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following line in a CREATE TABLE statement:

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

regclass在上面表示什么?

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

NB:是我的绝对要加 :: regclass 吗?

推荐答案

不,您不需要强制转换为 regclass 调用类似 regclass 参数的函数时,例如 nextval 是从文本 regclass 的隐式转换。在某些其他情况下,可能需要显式转换为 regclass

No, you do not need the cast to regclass when calling a function like nextval that accepts a regclass parameter, as there is an implict cast from text to regclass. In some other contexts an explicit cast to regclass may be required.

说明:

:: regclass 是强制类型转换,例如 :: integer

::regclass is a cast, like ::integer.

regclass 是一种魔术数据类型;它实际上是 oid 的别名,即对象标识符。请参阅文档中的对象标识符类型。强制转换为 regclass 是说此关系的名称,请将其转换为该关系的oid的快捷方式。强制转换为 regclass 知道 search_path ,与查询 pg_class 直接用于关系的 oid ,因此强制转换为regclass并不等同于子查询 pg_class

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

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

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

text regclass ,因此,如果省略了显式强制转换,并且您正在调用接受 regclass 强制转换的函数是自动完成的。因此,在 nextval 调用中,您不需要需要它。

There are implicit casts defined for text to regclass, so if you omit the explicit cast and you're calling a function that accepts regclass the cast is done automatically. So you do not need it in, for example, nextval calls.

在那里在其他地方。例如,您不能直接将文本 oid 进行比较;因此您可以执行以下操作:

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

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

但不是这样:

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';






只是为了好玩我试图写一个查询执行了等效的强制转换为 regclass 的操作。不要使用它,主要是为了娱乐,并试图演示实际发生的情况。除非您真的对Pg的肠道工作原理感兴趣,否则可以在这里停止阅读。


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.

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

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

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;

除了它要短得多而且要快得多。有关系统信息功能。 > current_schemas(...),等等。

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

换句话说:


  • 获取一个ab数组,列出我们有权访问的所有模式,并将每个条目与序号配对以表示其在数组中的位置

  • 搜索 pg_class 用于具有匹配名称的关系,并将每个关系与其名称空间(模式)相关联

  • 按其模式在其中出现的顺序对其余关系列表进行排序 search_path

  • 并选择第一个匹配项

  • 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_class for 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

这篇关于regclass在Postgresql中表示什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆