如何将 postgres pg_class reltablespace 设置为实际值,而不是 0?

How to set postgres pg_class reltablespace to the actual value, not 0?

我需要 pg_class.reltablespace 成为默认 table 空间的实际 oid 而不是 0。对于现有和未来新的 tables。我知道这是每个设计,但我需要实际的 oid 来实现自动化。

例如: 此 table 在默认命名空间

# select relname,reltablespace from pg_class where relname = '<my table>'   ;
      relname       | reltablespace 
--------------------+---------------
 <my table>         |             0

在我的例子中,它需要是“1663”:

# SELECT oid, spcname FROM pg_tablespace where spcname = 'pg_default';
 oid  |  spcname   
------+------------
 1663 | pg_default

是否有更改此行为的配置? 或者对获取 tablespace oid

的查询进行一些修改

如果关系的 table 空间为 0,这意味着 table 驻留在数据库的默认 table 空间中:

SELECT t.oid::regclass,
       CASE WHEN t.reltablespace <> 0
            THEN t.reltablespace
            ELSE d.dattablespace
       END
FROM pg_class AS t
   CROSS JOIN pg_database AS d
WHERE d.datname = current_database()
  AND t.relname = 'mytable';