如何编写 SQL 将 XML 文件读入 PostgreSQLXML值?
PostgreSQL 有一个原生XML 数据类型,具有将XMLPARSE文本字符串解析为该类型的函数。它还具有从文件系统读取数据的方法;在COPY声明中,等等。
但我没有看到编写原生 PostgreSQL SQL 语句来读取文件系统条目中的内容并使用它来填充XML值的方法。我怎样才能做到这一点?
我正在尝试设置一个具有有限权限的用户,该用户能够创建外部表。我有两个数据库,hr_db和accounting_db. 我已经创建了一个hr_user用户hr_db和一个accounting_user用户accounting_db。我只希望accounting_user用户对某些hr_db表(例如表)具有选择权限users。为此,作为超级用户,我转到hr_db数据库并运行:
GRANT CONNECT ON DATABASE hr_db TO accounting_user;
GRANT SELECT ON people TO accounting_user;
Run Code Online (Sandbox Code Playgroud)
我设置了一个连接hr_db从accounting_db使用外国数据包装:
CREATE SERVER hr_db FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', dbname 'hr_db', port '5432');
Run Code Online (Sandbox Code Playgroud)
然后我为accounting_user用户添加了一个映射:
CREATE USER MAPPING FOR accounting_user SERVER hr_db
OPTIONS (user 'accounting_user', password 'secretpassword');
Run Code Online (Sandbox Code Playgroud)
密码accounting_user与我用于从命令行登录的密码相同。这工作正常:
psql -U accounting_user -W hr_db
[enter accounting_user password] …Run Code Online (Sandbox Code Playgroud) 我正在查看7/01为 PostgreSQL安排的commit-fest,我看到 Pg 可能很快就会获得“身份列”。
我在information_schema.columns 中发现了一些提及但没什么
is_identity yes_or_no Applies to a feature not available in PostgreSQL
identity_generation character_data Applies to a feature not available in PostgreSQL
identity_start character_data Applies to a feature not available in PostgreSQL
identity_increment character_data Applies to a feature not available in PostgreSQL
identity_maximum character_data Applies to a feature not available in PostgreSQL
identity_minimum character_data Applies to a feature not available in PostgreSQL
identity_cycle yes_or_no Applies to a feature …Run Code Online (Sandbox Code Playgroud) 简单的数据库结构(用于在线论坛):
CREATE TABLE users (
id integer NOT NULL PRIMARY KEY,
username text
);
CREATE INDEX ON users (username);
CREATE TABLE posts (
id integer NOT NULL PRIMARY KEY,
thread_id integer NOT NULL REFERENCES threads (id),
user_id integer NOT NULL REFERENCES users (id),
date timestamp without time zone NOT NULL,
content text
);
CREATE INDEX ON posts (thread_id);
CREATE INDEX ON posts (user_id);
Run Code Online (Sandbox Code Playgroud)
表中大约有 8users万个条目和 260 万个条目posts。这个通过帖子获得前 100 位用户的简单查询需要2.4 秒:
EXPLAIN ANALYZE SELECT u.id, …Run Code Online (Sandbox Code Playgroud) [我认为这个问题的根本原因是我不了解权限和特权......]
所以,为了设置舞台,我的设置是一个数据库,称之为MyDb。
我有两个用户,spu1和u1,spu1是超级用户,u1是“普通”用户。MyDb的所有者是spu1。我想我还应该说u1具有从组角色继承的创建数据库和创建角色权限。
我有一个架构sch1,它是一个用户定义的架构。
在这个模式中,我有一个表,称为tbl1,还有一个物化视图,称为mvw1。
该TBL1的主人是SPU1,该mvw1的主人是U1。
在当前设置中,如上所述,我无法将mvw1刷新为u1或spu1。我只是得到以下有趣的错误(我已经广泛搜索了它,但没有找到任何可以完全解决我的设置的错误......)。
ERROR: permission denied for relation tbl1
********** Error **********
ERROR: permission denied for relation tbl1
SQL state: 42501
Run Code Online (Sandbox Code Playgroud)
我发现
我试图找出我需要授予普通用户u1缺少的权限(理想情况下所需的最低权限),以便我可以在以他们的身份登录时刷新此视图。
第一个选项虽然很高兴知道,但并不能解决我的问题。第二个选项似乎实际上是在向非超级用户授予超级用户权限,或者授予比我需要的更大的权限。
如果有人可以向我解释这里到底发生了什么(或指出我在解决问题所需的描述中遗漏了哪些信息),并让我知道我的第二个选择是否真的可行或更好的选择? …
postgresql permissions materialized-view errors postgresql-9.3
我有这个有效的 CTE 示例。
我可以选择所有祖父母和所有孩子。
但是如何在一个语句中选择所有祖父母和所有孩子?
在这个例子中,如果我给“父亲”作为输入,我想要祖父,父亲,儿子作为输出。
我使用 PostgreSQL。但我认为这个问题应该是标准的SQL。
如果我使用 PostgreSQL 特定的语法,请纠正我。
DROP table if exists tree;
CREATE TABLE tree (
id SERIAL PRIMARY KEY,
name character varying(64) NOT NULL,
parent_id integer REFERENCES tree NULL
);
insert into tree values (1, 'Grandfather', NULL);
insert into tree values (2, 'Father', 1);
insert into tree values (3, 'Son', 2);
-- -------------------------------------
-- Getting all children works
WITH RECURSIVE rec (id) as
(
SELECT tree.id, tree.name from tree where name='Father'
UNION ALL
SELECT tree.id, …Run Code Online (Sandbox Code Playgroud) 假设我想在两个日期之间生成一系列日期。我看到该功能generate_series仅提供
Function Argument Type Return Type Description
generate_series(start, stop, step interval) timestamp or timestamp with time zone setof timestamp or setof timestamp with time zone (same as argument type) Generate a series of values, from start to stop with a step size of step
Run Code Online (Sandbox Code Playgroud)
那么我该怎么做呢?
我有一个服务器和几个客户端应用程序。必须先启动服务器,然后才能启动客户端。如果它们不存在,客户端然后在数据库中创建表。
当服务器启动时(某些表不存在)并且以下查询给了我一个exception:
UPDATE recipes SET lock = null
WHERE lock IS NOT NULL;
Run Code Online (Sandbox Code Playgroud)
Relation >>recipes<< does not exists
我想exception通过检查此表是否存在来避免这种情况。
UPDATE recipes SET lock = null
WHERE lock IS NOT NULL AND
WHERE EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'recipes'
);
Run Code Online (Sandbox Code Playgroud)
但这query不起作用。你能告诉我我的错误在哪里吗?
PostgreSQL 内置的备份机制并不总是很合适。有时,您希望将应用程序置于静止状态,因为它具有您要在备份 PG 数据的同时进行备份的外部数据。但是将应用程序置于静止状态的唯一方法是“锁定”数据库。PG 缺乏数据库范围或集群范围的锁定机制。将 PG 置于只读状态将是以下解决方案中的一部分:
提高多达 1 亿行的表的读/写性能的常用方法是什么?
表有 column SEGMENT_ID INT NOT NULL,其中每个段有大约 100.000-1.000.000 行。写入 -SEGMENT_ID一次插入所有行,SEGMENT_ID之后不更新。读取 - 非常频繁,我需要良好的SELECT * FROM table WERE SEGMENT_ID = ?.
最明显的方法是SEGMENT_ID动态创建新表,但动态表意味着使用 ORM 甚至本机 SQL 查询框架进行黑客攻击。换句话说,你完成了有味道的代码。
您也可以使用分片,对吗?数据库是否在幕后创建新表?
我可以通过SEGMENT_ID. 但是,如果我一次插入所有与段相关的数据,我的插入是否会聚集在一起?
Postgres 还建议使用分区来处理非常大的表。
也许有某种神奇的索引可以帮助我避免动态创建新表或配置分片?
还有其他选择吗?
postgresql performance partitioning sharding performance-tuning
postgresql ×10
performance ×2
backup ×1
cte ×1
date ×1
datetime ×1
errors ×1
identity ×1
maintenance ×1
partitioning ×1
permissions ×1
recursive ×1
sharding ×1
timestamp ×1
timezone ×1
xml ×1