表中的每一行都有一个系统列 ctid,其类型tid表示该行的物理位置:
Run Code Online (Sandbox Code Playgroud)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;ctid | ID :---- | -: (0,1) | 1 (0,2) | 2
dbfiddle在这里
从ctid最合适的类型(例如integer,bigint或numeric(1000,0))中获取页码的最佳方法是什么?
在我能想到的唯一的办法是非常难看。
我有一项任务是将数组、记录以及在某些情况下的记录数组作为参数传递给 PostgreSQL 中的函数。
这是我的实际问题的一个最小示例:
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) 我想使用功能上类似于其他语言中的 ASSERT 例程,即构造(无论是过程,语法......)
ASSERT( <condition>, <msg>)
Run Code Online (Sandbox Code Playgroud)
这样,当<condition>传入的第一个参数为 false 时,会引发带有指定<msg>描述性消息的异常。
我知道手工做这件事很简单,但我想知道DBMS 是否提供了标准的。
必须自己编写一个或从 3rdy-party 包中导入一个是不切实际的,因为我需要它对我正在处理的每个项目都是完全可移植和透明的。
我可以通过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 语句做同样的事情?
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 …
是结合一个Postgres的方式IS DISTINCT FROM与ANY或得到同样结果的其他一些巧妙的方法?
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) 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.;实际上返回了正确的值。简洁到此为止。
我正在尝试两种方法来显示具有特定名称的列:
INFORMATION_SCHEMA.COLUMNS
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME='SUPPLIER_NAME';
Run Code Online (Sandbox Code Playgroud)系统列
SELECT *
FROM SYS.COLUMNS
WHERE NAME='SUPPLIER_NAME'
Run Code Online (Sandbox Code Playgroud)为什么查询显示不同的输出?
postgresql ×6
datatypes ×2
null ×2
sql-server ×2
array ×1
cast ×1
data-pages ×1
deadlock ×1
functions ×1
oracle ×1