- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 parent
和 child
表格如下:
create table parent
(
identifier serial primary key,
name text
);
create table child
(
identifier serial primary key,
name text, parent_identifier integer references parent
);
我创建了两个实用函数来格式化 parent
和 child
行到 JSON 对象:
create function format(child) returns json
as $$
select json_build_object('identifier', $1.identifier, 'name', $1.name)
$$ language sql stable;
create function format(parent) returns json
as $$
select json_build_object('identifier', $1.identifier, 'name', $1.name,
'children', array(select format(child) from child where parent_identifier = $1.identifier))
$$ language sql stable;
让我们测试一下:
insert into parent(name) values('first parent');
insert into parent(name) values('second parent');
insert into child(name, parent_identifier) values('first child first parent', (select identifier from parent where name = 'first parent'));
insert into child(name, parent_identifier) values('second child first parent', (select identifier from parent where name = 'first parent'));
insert into child(name, parent_identifier) values('first child second parent', (select identifier from parent where name = 'second parent'));
select format(parent) from parent;
这将返回以下 JSON 对象:
{
"identifier":5,
"name":"first parent",
"children":[
{
"identifier":7,
"name":"first child first parent"
},
{
"identifier":8,
"name":"second child first parent"
}
]
}
{
"identifier":6,
"name":"second parent",
"children":[
{
"identifier":9,
"name":"first child second parent"
}
]
}
太棒了!但是,这有一个大问题:如果另一笔交易在我们的 insert
之间做了一些更改。和 select
查询,select
查询确实返回我们刚刚插入的内容。我们可以通过将事务隔离级别设置为 repeatable read
来解决这个问题,但这有其性能成本和其他缺点(我们可能不得不重试)。
所以我考虑在一个 CTE 中重写上面的查询。如果我没记错的话,这不会遇到这样的并发问题。我开始如下:
with
parents as
(
insert into parent(name)
select 'first parent'
union all
select 'second parent'
returning parent.identifier, parent.name
),
children as
(
insert into child(name, parent_identifier)
select 'first child first parent', identifier from parents where name = 'first parent'
union all
select 'second child first parent', identifier from parents where name = 'first parent'
union all
select 'first child second parent', identifier from parents where name = 'second parent'
)
select format(parents::parent) from parents;
这没有按预期工作。它返回以下 JSON 对象:
{
"identifier":7,
"name":"first parent",
"children":[]
}
{
"identifier":8,
"name":"second parent",
"children":[]
}
如您所见,不包括子项。经过一番阅读,我明白发生了什么事。 CTE 处理在查询开始之前创建的快照。在 format(parent)
,我们正在做select format(child) from child where parent_identifier = $1.identifier)
,但这不会产生任何子行,因为子行不在快照中。所以我的问题不是这个,据我所知。
当然,如果我简单地执行 json_build_object
,我可以很容易地解决这个问题东西,与 format
中的完全相同功能,在主查询中,但后来我正在复制代码。我正在使用这些 format
在与此问题无关的其他查询中也起作用。理想情况下,我想在我的解决方案中避免代码重复。所以我想继续使用它们,可能需要先重构它们,以便它们可以在这个问题的场景中使用。
我现在被困住了。我真的很想继续使用 CTE(这样我就可以避免将事务隔离级别设置为 repeatable read
),但是我找不到重构 format(parent)
的方法和 format(child)
函数和/或 CTE,这样我就不会到处都是重复的代码。 SO 上有聪明的人有一些聪明的想法吗?
请注意,我使用的是 PostgreSQL 10.1。请在这里找到一个 fiddle :http://sqlfiddle.com/#!17/a251d/2
关于 Laurenz Albe 的回答的更新
上下文:https://stackoverflow.com/revisions/48152380/1
在上面的问题中,我确实是在简化我的情况。让我更详细地解释一下真实场景,但不要涉及太多会造成混淆的细节。
在该场景中,用户提供特定日期范围内的数据(= parents
及其相应的 children
),例如 2018 年 1 月。另外,我不只是在做插入,我实际上在做孤立行的更新和删除。因此,场景很简单:客户端正在替换给定日期范围内的所有数据。
如果我然后做 select format(parent) from parent where <parent is in date range as provided>
在更新和删除之后,其他一些客户可能已经更改了两者之间的重叠日期范围。在那种情况下,我将返回与客户端提供的不同结果,如果客户端未正确实现,这可能会引入错误。因此,这就是为什么我认为插入和选择需要是事务隔离级别设置为 repeatable read
的同一事务的一部分。 .
但后来,我开始考虑一个单一的、肥胖的 CTE,因此我的问题。
我希望这能澄清这个场景。
最佳答案
正如您注意到的那样,您无法在主 SELECT
的 CTE 中看到修改的行。这is documented :
The sub-statements in
WITH
are executed concurrently with each other and with the main query. Therefore, when using data-modifying statements inWITH
, the order in which the specified updates actually happen is unpredictable. All the statements are executed with the same snapshot (see Chapter 13), so they cannot “see” one another's effects on the target tables. This alleviates the effects of the unpredictability of the actual order of row updates, and means thatRETURNING
data is the only way to communicate changes between differentWITH
sub-statements and the main query.
所以你应该使用RETURNING
。
我想最简单的方法是不使用函数,而是在主查询中执行 json_build_object
并让它对 CTE parents
和 进行操作 children
。
关于sql - 重构函数,以便它们可以在 CTE 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151238/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!