Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

yb_pg_foreign_key bug #270

Open
jfunston opened this issue Jun 14, 2021 · 4 comments
Open

yb_pg_foreign_key bug #270

jfunston opened this issue Jun 14, 2021 · 4 comments
Labels
bug Something isn't working opengauss

Comments

@jfunston
Copy link
Contributor

test# ./regression.sh
src/k2/postgres/src/test/regress/chogori_schedule

*** 767,773 ****
-- Test for referencing column number smaller than referenced constraint
CREATE TABLE PKTABLE (ptest1 int, ptest2 int, UNIQUE(ptest1, ptest2));
CREATE TABLE FKTABLE_FAIL1 (ftest1 int REFERENCES pktable(ptest1));
! ERROR: there is no unique constraint matching given keys for referenced table "pktable"
SELECT COUNT(*) FROM pg_class WHERE relname = 'FKTABLE_FAIL1';
count

--- 767,773 ----
-- Test for referencing column number smaller than referenced constraint
CREATE TABLE PKTABLE (ptest1 int, ptest2 int, UNIQUE(ptest1, ptest2));
CREATE TABLE FKTABLE_FAIL1 (ftest1 int REFERENCES pktable(ptest1));
! ERROR: Invalid argument: Duplicate table
SELECT COUNT(*) FROM pg_class WHERE relname = 'FKTABLE_FAIL1';
count

@johnfangAFW
Copy link
Contributor

Added new task to add the support for foreign key validation in pg session:

#276

@johnfangAFW
Copy link
Contributor

(gdb) bt
#0 transformFkeyCheckAttrs (pkrel=pkrel@entry=0x7f75f029d360, numattrs=numattrs@entry=1,
attnums=attnums@entry=0x7ffdc8603c30, opclasses=opclasses@entry=0x7ffdc8603db0)
at /build/src/k2/postgres/src/backend/commands/tablecmds.c:8865
#1 0x000055d10d9ca3a4 in ATAddForeignKeyConstraint (wqueue=wqueue@entry=0x7ffdc8604130, tab=tab@entry=0x55d10f3d8cf0,
rel=rel@entry=0x7f75f02b9e60, fkconstraint=fkconstraint@entry=0x55d10f3d8fa0, parentConstr=parentConstr@entry=0,
recurse=recurse@entry=true, recursing=false, lockmode=6) at /build/src/k2/postgres/src/backend/commands/tablecmds.c:7553
#2 0x000055d10d9cae35 in ATExecAddConstraint (wqueue=0x7ffdc8604130, tab=0x55d10f3d8cf0, rel=rel@entry=0x7f75f02b9e60,
newConstraint=0x55d10f3d8fa0, recurse=recurse@entry=true, is_readd=is_readd@entry=false, lockmode=6)
at /build/src/k2/postgres/src/backend/commands/tablecmds.c:7254
#3 0x000055d10d9ce140 in ATExecCmd (wqueue=wqueue@entry=0x7ffdc8604130, tab=tab@entry=0x55d10f3d8cf0,
rel=rel@entry=0x7f75f02b9e60, cmd=0x55d10f3d8f48, lockmode=lockmode@entry=6)
at /build/src/k2/postgres/src/backend/commands/tablecmds.c:4201
#4 0x000055d10d9ce6f8 in ATRewriteCatalogs (wqueue=wqueue@entry=0x7ffdc8604130, lockmode=lockmode@entry=6)
at /build/src/k2/postgres/src/include/nodes/nodes.h:585
#5 0x000055d10d9ce8ad in ATController (parsetree=parsetree@entry=0x55d10f5cd638, rel=rel@entry=0x7f75f02b9e60,
cmds=, recurse=true, lockmode=lockmode@entry=6)
at /build/src/k2/postgres/src/backend/commands/tablecmds.c:3727
#6 0x000055d10d9ce951 in AlterTable (relid=relid@entry=16384, lockmode=lockmode@entry=6, stmt=stmt@entry=0x55d10f5cd638)
at /build/src/k2/postgres/src/backend/commands/tablecmds.c:3384
#7 0x000055d10dc3e4eb in ProcessUtilitySlow (pstate=pstate@entry=0x55d10fc30e48, pstmt=pstmt@entry=0x55d10f5ce528,
queryString=queryString@entry=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=context@entry=PROCESS_UTILITY_SUBCOMMAND, params=params@entry=0x0, queryEnv=queryEnv@entry=0x0,
dest=0x55d10e8527a0 , completionTag=0x0) at /build/src/k2/postgres/src/backend/tcop/utility.c:1150
#8 0x000055d10dc3dfc1 in standard_ProcessUtility (pstmt=0x55d10f5ce528,
queryString=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=PROCESS_UTILITY_SUBCOMMAND, params=0x0, queryEnv=0x0, dest=0x55d10e8527a0 , completionTag=0x0)
at /build/src/k2/postgres/src/backend/tcop/utility.c:949
#9 0x000055d10dc3dfe3 in K2PgProcessUtilityDefaultHook (pstmt=, queryString=,
context=, params=, queryEnv=, dest=, completionTag=0x0)
at /build/src/k2/postgres/src/backend/tcop/utility.c:3501
#10 0x000055d10ddaa6bc in K2PgTxnDdlProcessUtility (pstmt=0x55d10f5ce528,
queryString=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=PROCESS_UTILITY_SUBCOMMAND, params=0x0, queryEnv=0x0, dest=0x55d10e8527a0 , completionTag=0x0)
at /build/src/k2/postgres/src/backend/utils/misc/pg_k2pg_utils.c:848
#11 0x000055d10dc3e019 in ProcessUtility (pstmt=,
queryString=queryString@entry=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=context@entry=PROCESS_UTILITY_SUBCOMMAND, params=params@entry=0x0, queryEnv=queryEnv@entry=0x0,
dest=, completionTag=0x0) at /build/src/k2/postgres/src/backend/tcop/utility.c:370
#12 0x000055d10dc3e3a5 in ProcessUtilitySlow (pstate=pstate@entry=0x55d10f1df950, pstmt=pstmt@entry=0x55d10f1c0f48,
queryString=queryString@entry=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=context@entry=PROCESS_UTILITY_TOPLEVEL, params=params@entry=0x0, queryEnv=queryEnv@entry=0x0,
dest=0x55d10fc1e410, completionTag=0x7ffdc8604d30 "") at /build/src/k2/postgres/src/backend/tcop/utility.c:1093
#13 0x000055d10dc3dfc1 in standard_ProcessUtility (pstmt=0x55d10f1c0f48,
queryString=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x55d10fc1e410, completionTag=0x7ffdc8604d30 "")
at /build/src/k2/postgres/src/backend/tcop/utility.c:949
--Type for more, q to quit, c to continue without paging--
#14 0x000055d10dc3dfe3 in K2PgProcessUtilityDefaultHook (pstmt=, queryString=,
context=, params=, queryEnv=, dest=,
completionTag=0x7ffdc8604d30 "") at /build/src/k2/postgres/src/backend/tcop/utility.c:3501
#15 0x000055d10ddaa6bc in K2PgTxnDdlProcessUtility (pstmt=0x55d10f1c0f48,
queryString=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
context=PROCESS_UTILITY_TOPLEVEL, params=0x0, queryEnv=0x0, dest=0x55d10fc1e410, completionTag=0x7ffdc8604d30 "")
at /build/src/k2/postgres/src/backend/utils/misc/pg_k2pg_utils.c:848
#16 0x000055d10dc3e019 in ProcessUtility (pstmt=pstmt@entry=0x55d10f1c0f48, queryString=,
context=, params=, queryEnv=, dest=dest@entry=0x55d10fc1e410,
completionTag=0x7ffdc8604d30 "") at /build/src/k2/postgres/src/backend/tcop/utility.c:370
#17 0x000055d10dc39b5b in PortalRunUtility (portal=portal@entry=0x55d10f235000, pstmt=pstmt@entry=0x55d10f1c0f48,
isTopLevel=isTopLevel@entry=true, setHoldSnapshot=setHoldSnapshot@entry=false, dest=dest@entry=0x55d10fc1e410,
completionTag=completionTag@entry=0x7ffdc8604d30 "") at /build/src/k2/postgres/src/backend/tcop/pquery.c:1188
#18 0x000055d10dc3a9e6 in PortalRunMulti (portal=portal@entry=0x55d10f235000, isTopLevel=isTopLevel@entry=true,
setHoldSnapshot=setHoldSnapshot@entry=false, dest=dest@entry=0x55d10fc1e410, altdest=altdest@entry=0x55d10fc1e410,
completionTag=completionTag@entry=0x7ffdc8604d30 "") at /build/src/k2/postgres/src/backend/tcop/pquery.c:1348
#19 0x000055d10dc3b825 in PortalRun (portal=portal@entry=0x55d10f235000, count=count@entry=9223372036854775807,
isTopLevel=isTopLevel@entry=true, run_once=run_once@entry=true, dest=dest@entry=0x55d10fc1e410,
altdest=altdest@entry=0x55d10fc1e410, completionTag=0x7ffdc8604d30 "")
at /build/src/k2/postgres/src/backend/tcop/pquery.c:809
#20 0x000055d10dc363e3 in exec_simple_query (
query_string=query_string@entry=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));")
at /build/src/k2/postgres/src/backend/tcop/postgres.c:1160
#21 0x000055d10dc3670d in k2pg_exec_simple_query_attempting_to_restart_read (
query_string=query_string@entry=0x55d10f1bf190 "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1));",
exec_context=exec_context@entry=0x55d10f1bf070) at /build/src/k2/postgres/src/backend/tcop/postgres.c:4237
#22 0x000055d10dc39142 in PostgresMain (argc=, argv=argv@entry=0x55d10f1bbc98, dbname=,
username=0x55d10f1bbac8 "postgres") at /build/src/k2/postgres/src/backend/tcop/postgres.c:4838
#23 0x000055d10db98ce4 in BackendRun (port=port@entry=0x55d10f1e1180)
at /build/src/k2/postgres/src/backend/postmaster/postmaster.c:4444
#24 0x000055d10db9cbb0 in BackendStartup (port=port@entry=0x55d10f1e1180)
at /build/src/k2/postgres/src/backend/postmaster/postmaster.c:4097
#25 0x000055d10db9cec6 in ServerLoop () at /build/src/k2/postgres/src/backend/postmaster/postmaster.c:1740
#26 0x000055d10db9e9ce in PostmasterMain (argc=argc@entry=7, argv=argv@entry=0x55d10f1b6480)
at /build/src/k2/postgres/src/backend/postmaster/postmaster.c:1402
#27 0x000055d10da6d338 in PostgresServerProcessMain (argc=7, argv=0x55d10f1b6480)
at /build/src/k2/postgres/src/backend/main/main.c:234
#28 0x000055d10da70f98 in main ()

@johnfangAFW
Copy link
Contributor

tablecmds.c:8795

	/*
	 * Must have the right number of columns; must be unique and not a
	 * partial index; forget it if there are any expressions, too. Invalid
	 * indexes are out as well.
	 */
	if (indexStruct->indnkeyatts == numattrs &&
		indexStruct->indisunique &&
		IndexIsValid(indexStruct) &&
		heap_attisnull(indexTuple, Anum_pg_index_indpred, NULL) &&
		heap_attisnull(indexTuple, Anum_pg_index_indexprs, NULL))
	{

(gdb) p indexStruct->indnkeyatts
$16 = 2
(gdb) p numattrs
$17 = 1
(gdb) p indexStruct->indisunique
$18 = true

as we can see the table creation failed because the ddl "CREATE TABLE FKTABLE_FAIL3 (ftest1 int REFERENCES pktable(ptest1))" only reference to one of the two unique constraint columns, i.e., ptest1, which is not guaranteed to be unique.

@johnfangAFW
Copy link
Contributor

johnfangAFW commented Jun 24, 2021

postgres=# SELECT COUNT(*) FROM pg_class WHERE relname = 'FKTABLE_FAIL3';
count

 0

(1 row)

postgres=# select * from FKTABLE_FAIL3;
ERROR: relation "fktable_fail3" does not exist
LINE 1: select * from FKTABLE_FAIL3;
^

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working opengauss
Projects
None yet
Development

No branches or pull requests

3 participants