Do not generate update-specific code for multi link inserts
Link deletion and the conflict clause are update-specific, so don't
generate them in insert statements.
@@ -370,8 +370,9 @@ def process_insert_body(
# Process necessary updates to the link tables.
for shape_el, props_only in external_inserts:
process_link_update(
- ir_stmt, shape_el, props_only, wrapper,
- insert_cte, iterator_cte, ctx=ctx)
+ ir_stmt=ir_stmt, ir_set=shape_el, props_only=props_only,
+ wrapper=wrapper, dml_cte=insert_cte, iterator_cte=iterator_cte,
+ is_insert=True, ctx=ctx)
if parent_link_props:
prop_elements = []
@@ -538,7 +539,9 @@ def process_update_body(
ir_stmt, expr, wrapper, update_cte, ctx=ctx)
else:
process_link_update(
- ir_stmt, expr, False, wrapper, update_cte, None, ctx=ctx)
+ ir_stmt=ir_stmt, ir_set=expr, props_only=False,
+ wrapper=wrapper, dml_cte=update_cte, iterator_cte=None,
+ is_insert=False, ctx=ctx)
def is_props_only_update(shape_el: irast.Set, *,
@@ -558,12 +561,14 @@ def is_props_only_update(shape_el: irast.Set, *,
def process_link_update(
+ *,
ir_stmt: irast.MutatingStmt,
ir_set: irast.Set,
props_only: bool,
+ is_insert: bool,
wrapper: pgast.Query,
dml_cte: pgast.CommonTableExpr,
- iterator_cte: pgast.CommonTableExpr, *,
+ iterator_cte: pgast.CommonTableExpr,
ctx: context.CompilerContextLevel) -> typing.Optional[pgast.Query]:
"""Perform updates to a link relation as part of a DML statement.
@@ -620,6 +625,7 @@ def process_link_update(
dml_cte_rvar, ir_stmt.subject.path_id, env=ctx.env)
}
+ if not is_insert:
# Drop all previous link records for this source.
delcte = pgast.CommonTableExpr(
query=pgast.DeleteStmt(
@@ -669,6 +675,11 @@ def process_link_update(
]
)
+ cols = [pgast.ColumnRef(name=[col]) for col in specified_cols]
+
+ if is_insert:
+ conflict_clause = None
+ else:
# Inserting rows into the link table may produce cardinality
# constraint violations, since the INSERT into the link table
# is executed in the snapshot where the above DELETE from
@@ -702,14 +713,7 @@ def process_link_update(
)
)
- cols = [pgast.ColumnRef(name=[col]) for col in specified_cols]
- updcte = pgast.CommonTableExpr(
- name=ctx.env.aliases.get(hint='i'),
- query=pgast.InsertStmt(
- relation=target_rvar,
- select_stmt=data_select,
- cols=cols,
- on_conflict=pgast.OnConflictClause(
+ conflict_clause = pgast.OnConflictClause(
action='update',
infer=pgast.InferClause(
index_elems=conflict_inference
@@ -720,7 +724,15 @@ def process_link_update(
source=conflict_data
)
]
- ),
+ )
+
+ updcte = pgast.CommonTableExpr(
+ name=ctx.env.aliases.get(hint='i'),
+ query=pgast.InsertStmt(
+ relation=target_rvar,
+ select_stmt=data_select,
+ cols=cols,
+ on_conflict=conflict_clause,
returning_list=[
pgast.ResTarget(
val=pgast.ColumnRef(name=[pgast.Star()])
