假设我在行更新上执行以下函数:
CREATE OR REPLACE FUNCTION versioned_update()
RETURNS TRIGGER AS $$
DECLARE
sql TEXT;
BEGIN
sql := 'INSERT INTO backup_table VALUES (OLD)';
EXECUTE sql;
END;
$$ language 'plpgsql';
Run Code Online (Sandbox Code Playgroud)
上面的示例不起作用,因为OLD在该执行上下文中未知。所以我尝试了类似的方法:
sql := format('INSERT INTO backup_table VALUES (%L)', OLD);
Run Code Online (Sandbox Code Playgroud)
和
sql := format('INSERT INTO backup_table VALUES (%L)', (OLD));
Run Code Online (Sandbox Code Playgroud)
和
sql := format('INSERT INTO backup_table VALUES (%L)', (OLD.*));
Run Code Online (Sandbox Code Playgroud)
一切都没有任何运气。
这个问题是我正在研究的一个更大的触发因素的一部分。孤立来看,这没有多大意义,但说明了问题。
为什么我的 PostgreSQL 数据库文件夹中有以 .1 .2 .3 结尾的文件?
-rw------- 1 postgres postgres 1073741824 Oct 11 09:32 412rw95.2
-rw------- 1 postgres postgres 67100672 Oct 11 09:32 41295.3
-rw------- 1 postgres postgres 1073741824 Oct 11 09:07 41296
-rw------- 1 postgres postgres 1073741824 Oct 11 07:27 41296.1
Run Code Online (Sandbox Code Playgroud) 我创建了一个带有土耳其语排序规则的数据库。
\n\ncreatedb -l tr_TR test_tr -E LATIN5 -T template0\nRun Code Online (Sandbox Code Playgroud)\n\n然后是一张有一行的桌子。
\n\n\\c test_tr \ncreate table turkish(one text);\ninsert into turkish values(\'inci\');\nRun Code Online (Sandbox Code Playgroud)\n\n一切都在预料之中。
\n\ntest_tr=# \\l\n List of databases\n Name | Owner | Encoding | Collate | Ctype | Access privileges \n------------+----------+----------+-------------+-------------+-----------------------\n test_tr | postgres | LATIN5 | tr_TR | tr_TR | \nRun Code Online (Sandbox Code Playgroud)\n\n问题是,我期望 \xc4\xb0NC\xc4\xb0 但它给了我 INCI。这对于土耳其语来说是错误的。
\n\ntest_tr=# select upper("one") from turkish ;\n upper \n-------\n INCI\n(1 row)\nRun Code Online (Sandbox Code Playgroud)\n\n事实证明,默认排序规则仍然是英语。
\n\ntest_tr=# select upper("one" collate "tr_TR"),upper("one"), "one" from …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置到酒保服务器的流复制。
酒保配置非常基本,我正在使用backup_method = postgres.
[barman]
barman_home = /var/lib/barman
barman_user = barman
log_file = /var/log/barman/barman.log
log_level = DEBUG
archiver = true
backup_method = postgres
backup_options = concurrent_backup
compression = gzip
minimum_redundancy = 1
retention_policy = RECOVERY WINDOW OF 7 DAYS
retention_policy_mode = auto
streaming_archiver = true
wal_retention_policy = main
configuration_files_directory = /etc/barman.conf.d
Run Code Online (Sandbox Code Playgroud)
我会调用数据库服务器pg
[pg]
description = "pg"
ssh_command = ssh postgres@pg.example.com
conninfo = user=barman dbname=postgres host=pg.example.com port=5432
Run Code Online (Sandbox Code Playgroud)
我已经配置了 hba 规则并在's home中设置了一个.pgpass文件。barman/var/lib/barman/.pgpass
与服务器的测试连接pg工作正常: …
如何查看用户的 CONNECT 等数据库级别权限?
我已经找到了两种方法,但都不是完美的:
使用功能has_database_privilege()。
这可行,但我必须为每个用户一一手动运行它:
SELECT has_database_privilege('david', 'postgres', 'connect');
Run Code Online (Sandbox Code Playgroud)
这似乎返回了不完整的结果。它NULL首先返回,但david其他用户仍然可以连接到postgres数据库。仅当我在该数据库上运行任何GRANT命令(当然还有REVOKE)之后,该表才会填充一些结果:{=Tc/postgres,postgres=CTc/postgres}。所以我想我每次使用这种方法之前都必须GRANT这样做?REVOKE
SELECT datname as "Relation",
datacl as "Permissions"
FROM pg_database
WHERE datname = 'postgres';
Run Code Online (Sandbox Code Playgroud)
在 Ubuntu 上使用 PostgreSQL 10.9
我想了解增加是否work_mem有助于提高命令的速度COPY。
是否COPY使用work_mem或maint_work_mem广泛使用?
我主要是一名 Java 开发人员。今天我正在写一些 postgres 函数。出于习惯,我一直尝试使用类似 Javadoc 的注释风格来描述函数参数和返回类型来记录这些函数。是否有官方或广泛支持的模式来记录 postgres 函数?
假设您在 PostgreSQL 11 中有一个表,其中包含如下所示的值集。
12 18
11 12
5 12
9 18
7 19
14 18
11 16
6 12
4 11
5 9
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以在不使用子选择的情况下进行选择,或者通过使用函数或触发器/函数来查找每个值低于先前值或可能高于先前值的位置,但此示例低于先前值(可能是 2 行或15+ 行)在列和第一列中都高于最后一行或第一列和第二列以终止选择,使条件为正?
我不知道如何在选择中包含当前变量。如果我可以回顾变量那么我应该能够做到这一点。我想知道是否有人遇到过这个问题,并使其在选择中工作,或者他们在数据库之外进入 GoLang / Python / C++ / Perl / PHP / 等。一旦我离开数据库,做起来非常简单,但我我真的很想将其保留在 PostgreSQL 11 中。
我有一个由供应商提供的数据的大表(我无法对其进行太多更改),大约有 315 列。我怀疑许多列没有被使用(或者至少不一致)。
我想要一个查询,它可以为我提供表中每列值的计数。
例如
CREATE TABLE foo AS VALUES
( null , 'xyz' , 'pdq' , null ),
( 'abc' , 'def' , 'ghj' , null ),
( 'hsh' , 'fff' , 'oko' , null );
Run Code Online (Sandbox Code Playgroud)
所以这会产生类似的结果:
Col1 | 2
Col2 | 3
Col3 | 3
Col4 | 0
Run Code Online (Sandbox Code Playgroud)
编辑:澄清一下,我知道我可以使用,COUNT但我希望有一种方法可以首先循环遍历系统表的查询,以避免必须手动编写 315 count 语句。谢谢!
就像是
FOR column_names IN SELECT * FROM information_schema.columns WHERE
table_schema = 'public' AND table_name = 'vendor'
LOOP
RAISE NOTICE 'doing %s', quote_ident(column_names.column_name);
SELECT count(column_names.column_name) …Run Code Online (Sandbox Code Playgroud) PostgreSQL 的COPY命令对于快速导入大量数据非常有用,并且数据必须采用数据类型的文本表示形式。
我正在导入大量数据,包括一timestamp列,但它存储为“unix time”,即自纪元以来的秒数。我可以将其转换为ISO 8601(例如2010-01-01 00:00:00,并且 PostgreSQL 接受 a 的转换timestamp。它不接受原始纪元值整数。
是否可以让 postgres 接受纪元整数值并将其解释/转换为时间戳?这将使我的代码更简单(也许更快)。
这适用于psql:
create temporary table test1 ( v1 timestamp );
copy test1 from stdin ;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself, or an EOF signal.
>> 2010-01-01 00:00:00
>> \.
COPY 1
Run Code Online (Sandbox Code Playgroud)
但这些没有:
copy test1 from stdin …Run Code Online (Sandbox Code Playgroud) postgresql ×10
copy ×2
backup ×1
barman ×1
collation ×1
count ×1
functions ×1
import ×1
optimization ×1
performance ×1
permissions ×1
select ×1
timestamp ×1
trigger ×1