小编Jac*_*las的帖子

如何将ctid分解为页码和行号?

表中的每一行都有一个系统列 ctid,其类型tid表示该行的物理位置:

create table t(id serial);
insert into t default values;
insert into t default values;
Run Code Online (Sandbox Code Playgroud)
select ctid
     , id
from t;
Run Code Online (Sandbox Code Playgroud)
ctid | ID
:---- | -:
(0,1) | 1
(0,2) | 2

dbfiddle在这里

ctid最合适的类型(例如integerbigintnumeric(1000,0))中获取页码的最佳方法是什么?

我能想到的唯一的办法是非常难看。

postgresql data-pages datatypes cast postgresql-9.4

19
推荐指数
1
解决办法
1万
查看次数

将数组或记录传递给 PostgreSQL 中的函数?

我有一项任务是将数组、记录以及在某些情况下的记录数组作为参数传递给 PostgreSQL 中的函数。

postgresql functions array postgresql-9.0 composite-types

17
推荐指数
1
解决办法
4万
查看次数

'returning' 子句可以返回未插入的源列吗?

这是我的实际问题的一个最小示例:

create table t(id serial primary key, rnd double precision);
Run Code Online (Sandbox Code Playgroud)

当然,您可以使用returning子句返回插入的列:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *;
/*
| ID |            RND |
|----|----------------|
|  9 | 0.203221440315 |
*/
Run Code Online (Sandbox Code Playgroud)

你也可以返回一个文字:

with w as (insert into t(rnd) values(random()) returning *)
insert into t(rnd) select random() from w returning *, 1.0 dummy;
/*
| ID |            RND | DUMMY |
|----|----------------|-------|
| 11 | 0.594980469905 |     1 …
Run Code Online (Sandbox Code Playgroud)

postgresql postgresql-9.6

17
推荐指数
1
解决办法
9600
查看次数

Oracle PL/SQL 是否有标准的 ASSERT 例程?

我想使用功能上类似于其他语言中的 ASSERT 例程,即构造(无论是过程,语法......)

ASSERT( <condition>, <msg>)
Run Code Online (Sandbox Code Playgroud)

这样,当<condition>传入的第一个参数为 false 时,会引发带有指定<msg>描述性消息的异常。

我知道手工做这件事很简单,但我想知道DBMS 是否提供了标准的。

必须自己编写一个或从 3rdy-party 包中导入一个是不切实际的,因为我需要它对我正在处理的每个项目都是完全可移植和透明的。

oracle error-handling

16
推荐指数
2
解决办法
6323
查看次数

有没有办法将多行插入到表中,所有列都有默认值?

我可以通过RBAR方式将多行插入到一个表中,所有列的默认值:

create table course(course_id serial primary key);

do $$
begin
  for i in 1..100000 loop
    insert into course default values;
  end loop;
end;$$;
Run Code Online (Sandbox Code Playgroud)

有没有办法用单个 SQL 语句做同样的事情?

postgresql default-value postgresql-9.4

16
推荐指数
2
解决办法
2万
查看次数

同时调用同一个函数:死锁是如何发生的?

new_customerWeb 应用程序每秒调用我的函数数次(但每个会话仅调用一次)。它所做的第一件事就是锁定customer表(执行“如果不存在则插入”—— 的一个简单变体upsert)。

我对文档的理解是,其他调用new_customer应该简单地排队,直到所有先前的调用都完成:

LOCK TABLE 获得一个表级锁,必要时等待任何冲突的锁被释放。

为什么有时会死锁?

定义:

create function new_customer(secret bytea) returns integer language sql 
                security definer set search_path = postgres,pg_temp as $$
  lock customer in exclusive mode;
  --
  with w as ( insert into customer(customer_secret,customer_read_secret)
              select secret,decode(md5(encode(secret, 'hex')),'hex') 
              where not exists(select * from customer where customer_secret=secret)
              returning customer_id )
  insert into collection(customer_id) select customer_id from w;
  --
  select customer_id from customer where customer_secret=secret;
$$;
Run Code Online (Sandbox Code Playgroud)

日志中的错误:

2015-07-28 08:02:58 BST …

postgresql deadlock postgresql-9.4

15
推荐指数
1
解决办法
2264
查看次数

NULL 有类型吗?

各种来源(例如WikipediaPSOUG)声明 Oraclenull没有类型。这是真的?

其他 RDBMS 呢?

null datatypes

14
推荐指数
3
解决办法
903
查看次数

IS DISTINCT FROM 可以以某种方式与 ANY 或 ALL 结合吗?

是结合一个Postgres的方式IS DISTINCT FROMANY或得到同样结果的其他一些巧妙的方法?

select count(*)
from (select 'A' foo union all select 'Z' union all select null) z
where foo <> any(array[null, 'A']);

 count
-------
     1
(1 row)

select count(*)
from (select 'A' foo union all select 'Z' union all select null) z
where foo is distinct from any(array[null, 'A']);  

ERROR:  syntax error at or near "any"
LINE 3: where foo is distinct from any(array[null, 'A']);
                                   ^
Run Code Online (Sandbox Code Playgroud)

postgresql null postgresql-9.3

14
推荐指数
3
解决办法
1843
查看次数

为什么 TSQL 为 POWER(2.,64.) 返回错误的值?

select POWER(2.,64.)返回18446744073709552000而不是18446744073709551616. 它似乎只有 16 位精度(四舍五入第 17 位)。

即使使精度明确,select power(cast(2 as numeric(38,0)),cast(64 as numeric(38,0)))它仍然返回四舍五入的结果。

这似乎是一个非常基本的操作,因为它可以像这样以 16 位精度任意剥离。它可以正确计算的最高值仅为POWER(2.,56.),失败为POWER(2.,57.)。这里发生了什么?

真正可怕的是select 2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.*2.;实际上返回了正确的值。简洁到此为止。

sql-server azure-sql-database

14
推荐指数
2
解决办法
1717
查看次数

为什么 sys.columns 和 INFORMATION_SCHEMA.COLUMNS 显示不同的列数

我正在尝试两种方法来显示具有特定名称的列:

  1. INFORMATION_SCHEMA.COLUMNS

    SELECT *
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE COLUMN_NAME='SUPPLIER_NAME';
    
    Run Code Online (Sandbox Code Playgroud)
  2. 系统列

    SELECT *
    FROM SYS.COLUMNS
    WHERE NAME='SUPPLIER_NAME'
    
    Run Code Online (Sandbox Code Playgroud)

为什么查询显示不同的输出?

SSMS 屏幕抓取

sql-server

13
推荐指数
1
解决办法
2万
查看次数