postgresql - pg_catalog 表中某个字段的 NUMERIC 精度和比例在哪里?

标签 postgresql information-schema

在 PostgreSQL 中,表结构的列数据存储在 pg_attribute 中,一些字段在 pg_class 中,还有几个字段在 pg_attrdef 中。

但我没有看到任何地方存储的 NUMERIC 字段类型的精度或小数位数。

它可以在 INFORMATION_SCHEMA 表中找到,但我试图避免使用它们,因为它们不使用 oid 来轻松连接到 pg_catalog 表。

所以问题是: 列精度和小数位数存储在 postgreSQL 系统表中的什么位置?

最佳答案

它存储在 pg_attribute 中的 atttypmod 列中。所有信息都在 View information_schema.columns 中可用。此 View 使用一些查询来计算值,这些是最基本的:

SELECT
  CASE atttypid
         WHEN 21 /*int2*/ THEN 16
         WHEN 23 /*int4*/ THEN 32
         WHEN 20 /*int8*/ THEN 64
         WHEN 1700 /*numeric*/ THEN
              CASE WHEN atttypmod = -1
                   THEN null
                   ELSE ((atttypmod - 4) >> 16) & 65535     -- calculate the precision
                   END
         WHEN 700 /*float4*/ THEN 24 /*FLT_MANT_DIG*/
         WHEN 701 /*float8*/ THEN 53 /*DBL_MANT_DIG*/
         ELSE null
  END   AS numeric_precision,
  CASE 
    WHEN atttypid IN (21, 23, 20) THEN 0
    WHEN atttypid IN (1700) THEN            
        CASE 
            WHEN atttypmod = -1 THEN null       
            ELSE (atttypmod - 4) & 65535            -- calculate the scale  
        END
       ELSE null
  END AS numeric_scale,
  *
FROM 
    pg_attribute ;

关于postgresql - pg_catalog 表中某个字段的 NUMERIC 精度和比例在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350148/

相关文章:

mysql - mysql 查询可以在 information_schema 崩溃表上完成吗?

sql - 从列的文本中删除子字符串

ruby-on-rails - Rails - bool 字段未保存在 PostgreSQL/Heroku 上

sql - PostgreSQL 查询在生产中不使用索引

sql-server - 使用 information_schema 在 ms sql server 中查找外键

MySQL 不更新 information_schema,除非我手动运行 ANALYZE TABLE `myTable`

sql - 如何读取PostgreSQL EXPLAIN, order : top down or bottom up?

database - 错误 : function addgeometrycolumn is not unique

mysql - SQL 显示查询的反向过滤器。 “显示 xtable 中的字段,其中 Field = xFieldnameA = a AND Field = xFieldnameB

mysql - 来自 information_schema 的约束细节(关于更新级联,关于删除限制)