Skip to content

Commit

Permalink
sql: fix conupdtype, confdeltype, conmatchtype in pg_constraint
Browse files Browse the repository at this point in the history
Release note (bug fix): The columns `conupdtype`, `confdeltype` and
`conmatchtype` in `pg_constraint` now report the FK constraint
parameters properly, for compatibility with PostgreSQL clients that
use them.
  • Loading branch information
knz committed Feb 19, 2019
1 parent acba091 commit 1ea5603
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
58 changes: 58 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -1698,3 +1698,61 @@ SELECT conkey, confkey FROM pg_catalog.pg_constraint WHERE conname = 'my_fkey'
----
conkey confkey
{1} {1}

subtest regression_34862

statement ok
CREATE DATABASE d34862; SET database=d34862

statement ok
CREATE TABLE t(x INT UNIQUE);
CREATE TABLE u(
a INT REFERENCES t(x) ON DELETE NO ACTION,
b INT REFERENCES t(x) ON DELETE RESTRICT,
c INT REFERENCES t(x) ON DELETE SET NULL,
d INT DEFAULT 123 REFERENCES t(x) ON DELETE SET DEFAULT,
e INT REFERENCES t(x) ON DELETE CASCADE,
f INT REFERENCES t(x) ON UPDATE NO ACTION,
g INT REFERENCES t(x) ON UPDATE RESTRICT,
h INT REFERENCES t(x) ON UPDATE SET NULL,
i INT DEFAULT 123 REFERENCES t(x) ON UPDATE SET DEFAULT,
j INT REFERENCES t(x) ON UPDATE CASCADE
);

query TTT
SELECT conname, confupdtype, confdeltype FROM pg_constraint ORDER BY conname
----
fk_a_ref_t a a
fk_b_ref_t a r
fk_c_ref_t a n
fk_d_ref_t a d
fk_e_ref_t a c
fk_f_ref_t a a
fk_g_ref_t r a
fk_h_ref_t n a
fk_i_ref_t d a
fk_j_ref_t c a
t_x_key NULL NULL

statement ok
DROP TABLE u; DROP TABLE t

statement ok
CREATE TABLE v(x INT, y INT, UNIQUE (x,y))

statement ok
CREATE TABLE w(
a INT, b INT, c INT, d INT,
FOREIGN KEY (a,b) REFERENCES v(x,y) MATCH FULL,
FOREIGN KEY (c,d) REFERENCES v(x,y) MATCH SIMPLE
);

query TT
SELECT conname, confmatchtype FROM pg_constraint ORDER BY conname
----
fk_a_ref_v f
fk_c_ref_v s
v_x_y_key NULL

statement ok
DROP DATABASE d34862 CASCADE; SET database=test
31 changes: 22 additions & 9 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,18 +627,25 @@ var (
fkActionSetNull = tree.NewDString("n")
fkActionSetDefault = tree.NewDString("d")

// Avoid unused warning for constants.
_ = fkActionRestrict
_ = fkActionCascade
_ = fkActionSetNull
_ = fkActionSetDefault
fkActionMap = map[sqlbase.ForeignKeyReference_Action]tree.Datum{
sqlbase.ForeignKeyReference_NO_ACTION: fkActionNone,
sqlbase.ForeignKeyReference_RESTRICT: fkActionRestrict,
sqlbase.ForeignKeyReference_CASCADE: fkActionCascade,
sqlbase.ForeignKeyReference_SET_NULL: fkActionSetNull,
sqlbase.ForeignKeyReference_SET_DEFAULT: fkActionSetDefault,
}

fkMatchTypeFull = tree.NewDString("f")
fkMatchTypePartial = tree.NewDString("p")
fkMatchTypeSimple = tree.NewDString("s")

fkMatchMap = map[sqlbase.ForeignKeyReference_Match]tree.Datum{
sqlbase.ForeignKeyReference_SIMPLE: fkMatchTypeSimple,
sqlbase.ForeignKeyReference_FULL: fkMatchTypeFull,
// TODO(knz,bram): add when partial is supported.
// sqlbase.ForeignKeyReferenc_PARTIAL: fkMatchTypeSimple,
}
// Avoid unused warning for constants.
_ = fkMatchTypeFull
_ = fkMatchTypePartial
)

Expand Down Expand Up @@ -727,9 +734,15 @@ CREATE TABLE pg_catalog.pg_constraint (
contype = conTypeFK
conindid = h.IndexOid(referencedDB, tree.PublicSchema, con.ReferencedTable, con.ReferencedIndex)
confrelid = defaultOid(con.ReferencedTable.ID)
confupdtype = fkActionNone
confdeltype = fkActionNone
confmatchtype = fkMatchTypeSimple
if r, ok := fkActionMap[con.FK.OnUpdate]; ok {
confupdtype = r
}
if r, ok := fkActionMap[con.FK.OnDelete]; ok {
confdeltype = r
}
if r, ok := fkMatchMap[con.FK.Match]; ok {
confmatchtype = r
}
columnIDs := con.Index.ColumnIDs
if int(con.FK.SharedPrefixLen) > len(columnIDs) {
return pgerror.NewAssertionErrorf(
Expand Down
Loading

0 comments on commit 1ea5603

Please sign in to comment.