我有一个有数千行的表.由于表最初不是使用created_at列构造的,因此无法获取其创建时间戳.尽管开始获取未来行的时间戳是至关重要的.
有没有办法我可以添加一个默认值为NOW()的时间戳列,以便它不会将值填充到以前的行,但仅用于未来的行?
如果我执行ALTER查询,它会使用时间戳填充所有行:
ALTER TABLE mytable ADD COLUMN created_at TIMESTAMP DEFAULT NOW()
Run Code Online (Sandbox Code Playgroud) 免责声明:理论问题.
这里有几个问题,询问如何在PostgreSQL upsert语句中区分插入和更新的行.
这是一个简单的例子:
nd@postgres=# create table t(i int primary key, x int);
nd@postgres=# insert into t values(1,1);
nd@postgres=# insert into t values(1,11),(2,22)
on conflict(i) do update set x = excluded.i*11
returning *, xmin, xmax;
????????????????????????
? i ? x ? xmin ? xmax ?
????????????????????????
? 1 ? 11 ? 7696 ? 7696 ?
? 2 ? 22 ? 7696 ? 0 ?
????????????????????????
Run Code Online (Sandbox Code Playgroud)
所以,xmax> 0(或xmax= xmin) - 行已更新; xmax= 0 - …
我有一个理论问题,所以我对替代解决方案不感兴趣。对不起。
问:是否可以获取除当前行之外的所有先前行的窗口运行函数值?
例如:
with
t(i,x,y) as (
values
(1,1,1),(2,1,3),(3,1,2),
(4,2,4),(5,2,2),(6,2,8)
)
select
t.*,
sum(y) over (partition by x order by i) - y as sum,
max(y) over (partition by x order by i) as max,
count(*) filter (where y > 2) over (partition by x order by i) as cnt
from
t;
Run Code Online (Sandbox Code Playgroud)
实际结果是
i | x | y | sum | max | cnt
---+---+---+-----+-----+-----
1 | 1 | 1 | 0 | 1 | 0
2 | 1 | …Run Code Online (Sandbox Code Playgroud) explain analyse select true;
??????????????????????????????????????????????????????????????????????????????????????
? QUERY PLAN ?
??????????????????????????????????????????????????????????????????????????????????????
? Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.016..0.016 rows=1 loops=1) ?
? Planning time: 0.073 ms ?
? Execution time: 0.109 ms ?
??????????????????????????????????????????????????????????????????????????????????????
explain analyze select true;
??????????????????????????????????????????????????????????????????????????????????????
? QUERY PLAN ?
??????????????????????????????????????????????????????????????????????????????????????
? Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.004..0.005 rows=1 loops=1) ?
? Planning time: 0.030 ms ?
? Execution time: 0.036 ms ?
??????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
是功能还是记录功能(analyze = analyze)?
我正在学习Python和DJango,而且我对Linux很有用.当我创建DJango项目时,我有manage.py一个我可以执行的文件./manage.py runserver.然而,当我手动创建一些Python程序时,看起来我的Linux试图使用Bash而不是Python来执行它.所以,我需要写python foo.py代替./foo.py.这两个文件的属性manage.py和foo.py是相同的(-rwx--x---).所以我的问题是:差异在哪里以及如何在不指定的情况下执行python程序python?任何文件的链接都非常感谢.谢谢.
我做了从oracle到pgsql的数据库迁移,并得到了如下代码:
CREATE OR REPLACE FUNCTION PKG_UTIL_BD_LOGISTICS_getsignerinfo (
i_opCode T_MQ_LOGIC_TRACK_HEAD_LOG.LP_CODE%TYPE, i_remark T_MQ_LOGIC_TRACK_HEAD_LOG.REMARK%TYPE, i_acceptTime T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNED_TIME%TYPE, i_signer T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNER%TYPE, i_lpcode T_MQ_LOGIC_TRACK_HEAD_LOG.LP_CODE%TYPE,
o_signer OUT T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNER%TYPE, o_signerTime OUT T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNED_TIME%TYPE, o_status OUT T_MQ_LOGIC_TRACK_HEAD_LOG.STATUS%TYPE )
RETURNS RECORD AS $body$
DECLARE
v_signer T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNER%TYPE;
v_signerTime T_MQ_LOGIC_TRACK_HEAD_LOG.SIGNED_TIME%TYPE;
v_status T_MQ_LOGIC_TRACK_HEAD_LOG.STATUS%TYPE;
BEGIN
IF i_lpcode = 'SF' THEN
IF i_opCode = '8000' THEN
IF POSITION('?back' in i_remark) > 0 THEN
v_status := '3';
ELSE
v_status := '7';
v_signerTime := i_acceptTime;
v_signer := SUBSTR(i_remark, POSITION('?' in i_remark) + 1);
END IF;
ELSIF i_opCode = '9999' …Run Code Online (Sandbox Code Playgroud) 我玩pg_trgm扩展,我有点困惑。这是会议:
postgres=# create table t(i int, x text);
CREATE TABLE
postgres=# insert into t select i, random()::text from generate_series(1,50000000) as i;
INSERT 0 50000000
postgres=# explain analyze select * from t where x ilike '%666666%';
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..531870.29 rows=12954 width=36) (actual time=131.436..11408.176 rows=432 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t (cost=0.00..529574.89 rows=5398 width=36) (actual time=108.771..11304.946 rows=144 loops=3)
Filter: (x ~~* '%666666%'::text)
Rows Removed by Filter: 16666523
Planning Time: 0.121 ms …Run Code Online (Sandbox Code Playgroud) 我的问题与此有关:如何锁定表进行写入
我找到了简单的解决方案,但是我不确定副作用是否安全。
所以:
update pg_class set relkind = 'm' where relname = '<table_name>';
Run Code Online (Sandbox Code Playgroud)
(当然,考虑到表模式应该更复杂)
但是在我的简单测试中,它可以解决这个问题:
create table t(i int); insert into t values(1);
update pg_class set relkind = 'm' where relname = 't';
insert into t values(1);
-- ERROR: cannot change materialized view "t"
select * from t;
-- i
-- ---
-- 1
-- (1 row)
Run Code Online (Sandbox Code Playgroud)
因此,我的问题(目前仅是理论上的)是:此解决方案是否可能出问题?
postgresql ×7
database ×1
django ×1
filesystems ×1
linux ×1
plpgsql ×1
python ×1
sql ×1
upsert ×1