From 57ececd2d76e83709afc01c5112f6747f750ccf6 Mon Sep 17 00:00:00 2001 From: Junwang Zhao Date: Sat, 21 Dec 2024 12:10:03 +0000 Subject: [PATCH v1 1/3] fix wrong ref keys In propgraph_edge_get_ref_keys, when finding a matching foreign key, the fk pointer will always point to last ForeignKeyCacheInfo of the edge relation, which is wrong. We should add another pointer that remembers the matched ForeignKeyCacheInfo to ref_rel. Signed-off-by: Junwang Zhao --- src/backend/commands/propgraphcmds.c | 10 +++++++--- src/test/regress/expected/create_property_graph.out | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/propgraphcmds.c b/src/backend/commands/propgraphcmds.c index 8430bfda7b..b9afb3a413 100644 --- a/src/backend/commands/propgraphcmds.c +++ b/src/backend/commands/propgraphcmds.c @@ -381,6 +381,7 @@ propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols, const List ListCell *lc; int count = 0; ForeignKeyCacheInfo *fk = NULL; + ForeignKeyCacheInfo *fk2 = NULL; fkeys = RelationGetFKeyList(edge_rel); foreach(lc, fkeys) @@ -388,7 +389,10 @@ propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols, const List fk = lfirst_node(ForeignKeyCacheInfo, lc); if (fk->confrelid == RelationGetRelid(ref_rel)) + { count++; + fk2 = fk; + } } if (count == 0) @@ -402,10 +406,10 @@ propgraph_edge_get_ref_keys(ParseState *pstate, const List *keycols, const List errmsg("more than one suitable foreign key exists for %s key of edge \"%s\"", type, aliasname), parser_errposition(pstate, location)); - Assert(fk); + Assert(fk2); - *outkey = array_from_attnums(fk->nkeys, fk->conkey); - *outref = array_from_attnums(fk->nkeys, fk->confkey); + *outkey = array_from_attnums(fk2->nkeys, fk2->conkey); + *outref = array_from_attnums(fk2->nkeys, fk2->confkey); } } diff --git a/src/test/regress/expected/create_property_graph.out b/src/test/regress/expected/create_property_graph.out index 43316fbc02..70c4afda52 100644 --- a/src/test/regress/expected/create_property_graph.out +++ b/src/test/regress/expected/create_property_graph.out @@ -96,7 +96,7 @@ SELECT pg_get_propgraphdef('g5'::regclass); t12 KEY (b) PROPERTIES (b) + ) + EDGE TABLES ( + - t13 KEY (c) SOURCE KEY (e) REFERENCES t11 (a) DESTINATION KEY (e) REFERENCES t12 (b) PROPERTIES (c, d, e)+ + t13 KEY (c) SOURCE KEY (d) REFERENCES t11 (a) DESTINATION KEY (e) REFERENCES t12 (b) PROPERTIES (c, d, e)+ ) (1 row) @@ -255,7 +255,7 @@ SELECT * FROM information_schema.pg_edge_table_components ORDER BY property_grap regression | create_property_graph_tests | g4 | e2 | t1 | SOURCE | a | a | 1 regression | create_property_graph_tests | g4 | e2 | t3 | DESTINATION | x | x | 1 regression | create_property_graph_tests | g4 | e2 | t3 | DESTINATION | t | y | 2 - regression | create_property_graph_tests | g5 | t13 | t11 | SOURCE | e | a | 1 + regression | create_property_graph_tests | g5 | t13 | t11 | SOURCE | d | a | 1 regression | create_property_graph_tests | g5 | t13 | t12 | DESTINATION | e | b | 1 (12 rows) -- 2.39.5