我的表上有 100 多个索引。我想临时禁用所有索引,并希望在索引完成后重新启用它们。
键入每个索引名称来禁用它是不可行的。是否有任何 PostgreSQL 语句可以临时禁用所有索引,然后再次重新启用它们。
问候,
我惊讶地发现is_nullable此查询的列被键入character varying (3)而不是boolean:
select
column_name,
data_type,
is_nullable
from
information_schema.columns
where
table_schema = 'public';
Run Code Online (Sandbox Code Playgroud)
我注意到其他一些列也使用“是”/“否”。这样做的理由是什么?我最初的想法是,也许除了 "YES" 和 "NO" 之外的其他值也是可能的,证明 a 以外的其他值是合理的boolean,但我在当前的数据库中没有看到任何这样的例子。
当一次只能进行一个引用时,我正在寻找在一个表中引用多个表的最有效方法。这意味着表 A 和 B 被表 C 引用,但 A 和 B 不能在 C 的一行中引用,并且我在编写查询时选择要查看的表。
我考虑过 4 种方法:
type和一个列FK,这样我就可以进行像这样的连接type = 'A' AND a.pk = c.a_fk(这就是我们现在使用的)FROM c JOIN a ON a.d_fk = c.d_fk在我尝试的每个解决方案中,查询规划器对于将返回多少行的判断都是错误的。
这是一个例子:我创建了一个简单的数据库,其中有 3 个表,如下所示:
Table "public.a"
Column | Type | Collation | Nullable | Default
--------+--------+-----------+----------+-------------------------------
pk | bigint | | not null | …Run Code Online (Sandbox Code Playgroud) postgresql performance database-design postgresql-9.6 postgresql-11 postgresql-performance
也许我在这里遗漏了一些东西:
CREATE TABLE public.example_table (
id integer UNIQUE
);
CREATE TABLE public.foreign_table (
id integer,
example_table_id integer,
CONSTRAINT fk_example_table_id
FOREIGN KEY (example_table_id)
REFERENCES public.example_table (id)
ON DELETE SET NULL
);
INSERT INTO public.example_table (id) VALUES
(1);
INSERT INTO public.foreign_table (id, example_table_id) VALUES
(1, 1),
(2, null);
Run Code Online (Sandbox Code Playgroud)
如果我运行TRUNCATE CASCADE,两个表都会被擦除,这不是我预期会发生的情况。
TRUNCATE example_table CASCADE;
SELECT COUNT(*) FROM public.foreign_table;
0
Run Code Online (Sandbox Code Playgroud)
我期望发生的情况会foreign_table改变为:
(1, null)
(2, null)
Run Code Online (Sandbox Code Playgroud)
我是否不明白 SET NULL 应该完成什么?
有没有办法使用 TRUNCATE CASCADE 而不将其从另一个表中删除?我在可以调用的地方使用 Laravel Model::truncate();,它会自动截断表并重置我的索引,我希望我可以调用它example_table并让它重置所有行foreign_table, …
PostgreSQL 版本:我的本地安装是 11.3,下面的小提琴是 10.0。两者的行为相同。
我有一个页面架构,每个页面都有部分,每个部分可以有不同类型的内容。当我查询某个页面时,我希望在 JSON 文档中输出有关该页面的所有信息。
我将 CTE 用于json_agg()每个部分的各种内容。最后,我将各个部分连接起来json_object_agg(),将部分标题映射到部分内容。
问题: json_object_agg()当页面没有任何部分时抛出错误。我已经通过使用没有章节标题的常规来验证有罪json_agg()。确切的错误:
error: field name must not be null
Run Code Online (Sandbox Code Playgroud)
我想要的:无论如何都不是错误。我不想在接收方进行自定义错误处理。如果查询可以返回 JSONNull来代替json_object_agg()没有部分的情况,那就更好了,但这是可选的。(欢迎其他优雅的解决方案)
文档
可能文档不完整或者我遗漏了一些东西。仅供参考。
在聚合表达式上它说(强调我的):
大多数聚合函数都会忽略 null 输入,因此一个或多个表达式产生null 的行将被丢弃。除非另有说明,对于所有内置聚合,这可以 假定为 true 。
在聚合函数中,json_object_agg()没有评论不处理null:
将名称/值对聚合为 JSON 对象
摆弄错误的域参数。将域更改为其他选项可以使其正常工作。使用不存在的域也可以正常工作并返回 0 行。
询问
with secs as (
select p.page_id, p.domain, s.section_id as sid, s.title as title
from pages p
left …Run Code Online (Sandbox Code Playgroud) 我有一个表,每天都会添加大约 2000 万条记录,我试图对其进行分页,以便人们可以访问其中的所有数据,但查询时间必须“合适”(在我的例子中)定义为少于 30 秒/查询)。
为此,我过去一直使用键集分页,但对于这个特定的查询和表,我的查询时间非常慢,这似乎是因为查询规划器决定过滤掉一天的数据,然后对其运行过滤器而不是索引条件扫描。
该表如下所示:
create table mmsi_positions_archive
(
id bigserial not null
constraint mmsi_positions_archive_pkey
primary key,
position_id uuid,
previous_id uuid,
mmsi bigint not null,
collection_type varchar not null,
accuracy numeric,
maneuver numeric,
rate_of_turn numeric,
status integer,
speed numeric,
course numeric,
heading numeric,
position geometry(Point,4326),
timestamp timestamp with time zone not null,
updated_at timestamp with time zone default now(),
created_at timestamp with time zone default now()
);
create index ix_mmsi_positions_archive_mmsi
on mmsi_positions_archive (mmsi);
create index ix_mmsi_positions_archive_position_id
on mmsi_positions_archive …Run Code Online (Sandbox Code Playgroud) postgresql performance postgresql-9.6 postgresql-performance
也许双重帖子,但我在这里找不到任何答案。
我看到了几篇如何命名我的表的帖子,我明白(半官方)它应该包含小写字母,应该下划线和单数(fe my_table)
当我必须执行以下操作时,我的问题出现了。我有一个应用程序,其中有用户、平台和 user_info,其中平台是用户正在使用的内容(例如 MAC、PC、LINUX)。如果我需要在用户和平台之间创建联结表,我该如何命名它,以便下一个浏览模式的人了解用户和平台有一个名为 user_platform 的联结表,但当他看到表 user_info 时不会感到困惑?
BR
我正在尝试配置最新的 pgbouncer 以与 postgres 9 一起使用。我可以使用psql正确的密码连接到我的数据库,但是当我使用时,psql -p 6432我无法连接,错误消息为psql: ERROR: auth failed
这似乎可能是由我的 userlist.txt 文件引起的,但我不确定。我检查过,所有必需的文件均由 Postgres 系统用户完全拥有
pgbouncer.ini
[databases]
postgres = host=localhost port=5433 auth_user=postgres dbname=postgres
[pgbouncer]
pidfile = /usr/local/pgbouncer-1.9.0/pgbouncer.pid
logfile = /usr/local/pgbouncer-1.9.0/log/pgbouncer.log
user = postgres
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = /usr/local/pgbouncer-1.9.0/etc/userlist.txt
Run Code Online (Sandbox Code Playgroud)
用户列表.txt
"postgres" "md5<MD5 SUM>"
用于启动 pgbouncer 的命令
./bin/pgbouncer -d etc/pgbouncer.ini
日志输出显示失败
2019-08-20 13:46:01.080 16446 LOG C-0x1028ce0: postgres/postgres@127.0.0.1:43286 login attempt: db=postgres user=postgres tls=no
2019-08-20 13:46:01.080 16446 LOG C-0x1028ce0: postgres/postgres@127.0.0.1:43286 closing …Run Code Online (Sandbox Code Playgroud) 我正在尝试对 RDS 实例上的 PostgreSQL 数据库进行我认为常见的设置。我希望有:
public架构上,还是在自定义架构上,我都不太关心GRANT我已尝试以下操作但无济于事,postgres用户是由 RDS 创建的具有角色的用户rds_superuser:
-- Making sure roles can't do anything on the database without explicit grant
REVOKE CREATE ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON DATABASE my_database FROM PUBLIC;
CREATE SCHEMA my_schema;
-- Read-write
CREATE ROLE read_write;
GRANT CONNECT ON DATABASE my_database TO read_write;
GRANT USAGE, CREATE ON SCHEMA my_schema TO read_write;
GRANT SELECT, INSERT, UPDATE, …Run Code Online (Sandbox Code Playgroud) 我想使用 TABLESAMPLE 从 PostgreSQL 满足特定条件的行中随机采样。
这运行良好:
select * from customers tablesample bernoulli (1);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将条件嵌入脚本中。例如这个
select * from customers where last_name = 'powell' tablesample bernoulli (1);
Run Code Online (Sandbox Code Playgroud)
抛出这个错误:
SQL 错误 [42601]:错误:“tablesample”位置或附近的语法错误
:71
postgresql ×10
performance ×2
aggregate ×1
amazon-rds ×1
json ×1
permissions ×1
pgbouncer ×1
query ×1
random ×1
sql-standard ×1
table ×1
truncate ×1
where ×1