8000 Fix remaining RI permission problems (cascaded update/delete, restrict, · danielcode/postgres@c33b72e · GitHub
[go: up one dir, main page]

Skip to content

Commit c33b72e

Browse files
committed
Fix remaining RI permission problems (cascaded update/delete, restrict,
set null/default).
1 parent 931e309 commit c33b72e

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/backend/utils/adt/ri_triggers.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 2000-2001, PostgreSQL Global Development Group
1919
* Copyright 1999 Jan Wieck
2020
*
21-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.23 2001/03/22 06:16:17 momjian Exp $
21+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.23.2.1 2001/05/09 16:28:31 petere Exp $
2222
*
2323
* ----------
2424
*/
@@ -941,6 +941,8 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
941941
char del_nulls[RI_MAX_NUMKEYS + 1];
942942
bool isnull;
943943
int i;
944+
Oid save_uid;
945+
Oid fk_owner;
944946

945947
ReferentialIntegritySnapshotOverride = true;
946948

@@ -978,6 +980,7 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
978980
* tuple.
979981
*/
980982
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
983+
fk_owner = RelationGetForm(fk_rel)->relowner;
981984
pk_rel = trigdata->tg_relation;
982985
old_row = trigdata->tg_trigtuple;
983986

@@ -1081,9 +1084,14 @@ RI_FKey_cascade_del(PG_FUNCTION_ARGS)
10811084
/*
10821085
* Now delete constraint
10831086
*/
1087+
save_uid = GetUserId();
1088+
SetUserId(fk_owner);
1089+
10841090
if (SPI_execp(qplan, del_values, del_nulls, 0) != SPI_OK_DELETE)
10851091
elog(ERROR, "SPI_execp() failed in RI_FKey_cascade_del()");
10861092

1093+
SetUserId(save_uid);
1094+
10871095
if (SPI_finish() != SPI_OK_FINISH)
10881096
elog(NOTICE, "SPI_finish() failed in RI_FKey_cascade_del()");
10891097

@@ -1128,6 +1136,8 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
11281136
bool isnull;
11291137
int i;
11301138
int j;
1139+
Oid save_uid;
1140+
Oid fk_owner;
11311141

11321142
ReferentialIntegritySnapshotOverride = true;
11331143

@@ -1165,6 +1175,7 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
11651175
* and old tuple.
11661176
*/
11671177
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
1178+
fk_owner = RelationGetForm(fk_rel)->relowner;
11681179
pk_rel = trigdata->tg_relation;
11691180
new_row = trigdata->tg_newtuple;
11701181
old_row = trigdata->tg_trigtuple;
@@ -1297,9 +1308,14 @@ RI_FKey_cascade_upd(PG_FUNCTION_ARGS)
12971308
/*
12981309
* Now update the existing references
12991310
*/
1311+
save_uid = GetUserId();
1312+
SetUserId(fk_owner);
1313+
13001314
if (SPI_execp(qplan, upd_values, upd_nulls, 0) != SPI_OK_UPDATE)
13011315
elog(ERROR, "SPI_execp() failed in RI_FKey_cascade_upd()");
13021316

1317+
SetUserId(save_uid);
1318+
13031319
if (SPI_finish() != SPI_OK_FINISH)
13041320
elog(NOTICE, "SPI_finish() failed in RI_FKey_cascade_upd()");
13051321

@@ -1349,6 +1365,8 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
13491365
char del_nulls[RI_MAX_NUMKEYS + 1];
13501366
bool isnull;
13511367
int i;
1368+
Oid save_uid;
1369+
Oid fk_owner;
13521370

13531371
ReferentialIntegritySnapshotOverride = true;
13541372

@@ -1386,6 +1404,7 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
13861404
* tuple.
13871405
*/
13881406
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
1407+
fk_owner = RelationGetForm(fk_rel)->relowner;
13891408
pk_rel = trigdata->tg_relation;
13901409
old_row = trigdata->tg_trigtuple;
13911410

@@ -1493,9 +1512,14 @@ RI_FKey_restrict_del(PG_FUNCTION_ARGS)
14931512
/*
14941513
* Now check for existing references
14951514
*/
1515+
save_uid = GetUserId();
1516+
SetUserId(fk_owner);
1517+
14961518
if (SPI_execp(qplan, del_values, del_nulls, 1) != SPI_OK_SELECT)
14971519
elog(ERROR, "SPI_execp() failed in RI_FKey_restrict_del()");
14981520

1521+
SetUserId(save_uid);
1522+
14991523
if (SPI_processed > 0)
15001524
elog(ERROR, "%s referential integrity violation - "
15011525
"key in %s still referenced from %s",
@@ -1554,8 +1578,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
15541578
bool isnull;
15551579
int i;
15561580
Oid save_uid;
1557-
1558-
save_uid = GetUserId();
1581+
Oid fk_owner;
15591582

15601583
ReferentialIntegritySnapshotOverride = true;
15611584

@@ -1593,6 +1616,7 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
15931616
* and old tuple.
15941617
*/
15951618
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
1619+
fk_owner = RelationGetForm(fk_rel)->relowner;
15961620
pk_rel = trigdata->tg_relation;
15971621
new_row = trigdata->tg_newtuple;
15981622
old_row = trigdata->tg_trigtuple;
@@ -1708,6 +1732,9 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
17081732
/*
17091733
* Now check for existing references
17101734
*/
1735+
save_uid = GetUserId();
1736+
SetUserId(fk_owner);
1737+
17111738
SetUserId(RelationGetForm(pk_rel)->relowner);
17121739

17131740
if (SPI_execp(qplan, upd_values, upd_nulls, 1) != SPI_OK_SELECT)
@@ -1764,6 +1791,8 @@ RI_FKey_setnull_del(PG_FUNCTION_ARGS)
17641791
char upd_nulls[RI_MAX_NUMKEYS + 1];
17651792
bool isnull;
17661793
int i;
1794+
Oid save_uid;
1795+
Oid fk_owner;
17671796

17681797
ReferentialIntegritySnapshotOverride = true;
17691798

@@ -1801,6 +1830,7 @@ RI_FKey_setnull_del(PG_FUNCTION_ARGS)
18011830
* tuple.
18021831
*/
18031832
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
1833+
fk_owner = RelationGetForm(fk_rel)->relowner;
18041834
pk_rel = trigdata->tg_relation;
18051835
old_row = trigdata->tg_trigtuple;
18061836

@@ -1915,9 +1945,14 @@ RI_FKey_setnull_del(PG_FUNCTION_ARGS)
19151945
/*
19161946
* Now update the existing references
19171947
*/
1948+
save_uid = GetUserId();
1949+
SetUserId(fk_owner);
1950+
19181951
if (SPI_execp(qplan, upd_values, upd_nulls, 0) != SPI_OK_UPDATE)
19191952
elog(ERROR, "SPI_execp() failed in RI_FKey_setnull_del()");
19201953

1954+
SetUserId(save_uid);
1955+
19211956
if (SPI_finish() != SPI_OK_FINISH)
19221957
elog(NOTICE, "SPI_finish() failed in RI_FKey_setnull_del()");
19231958

@@ -1963,6 +1998,8 @@ RI_FKey_setnull_upd(PG_FUNCTION_ARGS)
19631998
int i;
19641999
int match_type;
19652000
bool use_cached_query;
2001+
Oid save_uid;
2002+
Oid fk_owner;
19662003

19672004
ReferentialIntegritySnapshotOverride = true;
19682005

@@ -2000,6 +2037,7 @@ RI_FKey_setnull_upd(PG_FUNCTION_ARGS)
20002037
* tuple.
20012038
*/
20022039
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
2040+
fk_owner = RelationGetForm(fk_rel)->relowner;
20032041
pk_rel = trigdata->tg_relation;
20042042
new_row = trigdata->tg_newtuple;
20052043
old_row = trigdata->tg_trigtuple;
@@ -2161,9 +2199,14 @@ RI_FKey_setnull_upd(PG_FUNCTION_ARGS)
21612199
/*
21622200
* Now update the existing references
21632201
*/
2202+
save_uid = GetUserId();
2203+
SetUserId(fk_owner);
2204+
21642205
if (SPI_execp(qplan, upd_values, upd_nulls, 0) != SPI_OK_UPDATE)
21652206
elog(ERROR, "SPI_execp() failed in RI_FKey_setnull_upd()");
21662207

2208+
SetUserId(save_uid);
2209+
21672210
if (SPI_finish() != SPI_OK_FINISH)
21682211
elog(NOTICE, "SPI_finish() failed in RI_FKey_setnull_upd()");
21692212

@@ -2206,6 +2249,8 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
22062249
char upd_nulls[RI_MAX_NUMKEYS + 1];
22072250
bool isnull;
22082251
int i;
2252+
Oid save_uid;
2253+
Oid fk_owner;
22092254

22102255
ReferentialIntegritySnapshotOverride = true;
22112256

@@ -2243,6 +2288,7 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
22432288
* tuple.
22442289
*/
22452290
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
2291+
fk_owner = RelationGetForm(fk_rel)->relowner;
22462292
pk_rel = trigdata->tg_relation;
22472293
old_row = trigdata->tg_trigtuple;
22482294

@@ -2404,9 +2450,14 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
24042450
/*
24052451
* Now update the existing references
24062452
*/
2453+
save_uid = GetUserId();
2454+
SetUserId(fk_owner);
2455+
24072456
if (SPI_execp(qplan, upd_values, upd_nulls, 0) != SPI_OK_UPDATE)
24082457
elog(ERROR, "SPI_execp() failed in RI_FKey_setdefault_del()");
24092458

2459+
SetUserId(save_uid);
2460+
24102461
if (SPI_finish() != SPI_OK_FINISH)
24112462
elog(NOTICE, "SPI_finish() failed in RI_FKey_setdefault_del()");
24122463

@@ -2451,6 +2502,8 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
24512502
bool isnull;
24522503
int i;
24532504
int match_type;
2505+
Oid save_uid;
2506+
Oid fk_owner;
24542507

24552508
ReferentialIntegritySnapshotOverride = true;
24562509

@@ -2488,6 +2541,7 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
24882541
* tuple.
24892542
*/
24902543
fk_rel = heap_openr(tgargs[RI_FK_RELNAME_ARGNO], NoLock);
2544+
fk_owner = RelationGetForm(fk_rel)->relowner;
24912545
pk_rel = trigdata->tg_relation;
24922546
new_row = trigdata->tg_newtuple;
24932547
old_row = trigdata->tg_trigtuple;
@@ -2676,9 +2730,14 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
26762730
/*
26772731
* Now update the existing references
26782732
*/
2733+
save_uid = GetUserId();
2734+
SetUserId(fk_owner);
2735+
26792736
if (SPI_execp(qplan, upd_values, upd_nulls, 0) != SPI_OK_UPDATE)
26802737
elog(ERROR, "SPI_execp() failed in RI_FKey_setdefault_upd()");
26812738

2739+
SetUserId(save_uid);
2740+
26822741
if (SPI_finish() != SPI_OK_FINISH)
26832742
elog(NOTICE, "SPI_finish() failed in RI_FKey_setdefault_upd()");
26842743

0 commit comments

Comments
 (0)
0