Commit c0cba9ef authored by Peter Eisentraut's avatar Peter Eisentraut

When the RI triggers lock the PK table, temporarily switch the current user

id to the owner of the PK table, to avoid permission problems.
parent f428d053
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* *
* 1999 Jan Wieck * 1999 Jan Wieck
* *
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.16 2000/05/30 00:49:53 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.17 2000/09/25 22:34:20 petere Exp $
* *
* ---------- * ----------
*/ */
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "catalog/pg_operator.h" #include "catalog/pg_operator.h"
#include "commands/trigger.h" #include "commands/trigger.h"
#include "executor/spi_priv.h" #include "executor/spi_priv.h"
#include "miscadmin.h"
/* ---------- /* ----------
...@@ -158,6 +159,9 @@ RI_FKey_check(PG_FUNCTION_ARGS) ...@@ -158,6 +159,9 @@ RI_FKey_check(PG_FUNCTION_ARGS)
bool isnull; bool isnull;
int i; int i;
int match_type; int match_type;
Oid save_uid;
save_uid = GetUserId();
ReferentialIntegritySnapshotOverride = true; ReferentialIntegritySnapshotOverride = true;
...@@ -252,9 +256,13 @@ RI_FKey_check(PG_FUNCTION_ARGS) ...@@ -252,9 +256,13 @@ RI_FKey_check(PG_FUNCTION_ARGS)
if (SPI_connect() != SPI_OK_CONNECT) if (SPI_connect() != SPI_OK_CONNECT)
elog(NOTICE, "SPI_connect() failed in RI_FKey_check()"); elog(NOTICE, "SPI_connect() failed in RI_FKey_check()");
SetUserId(RelationGetForm(pk_rel)->relowner);
if (SPI_execp(qplan, check_values, check_nulls, 1) != SPI_OK_SELECT) if (SPI_execp(qplan, check_values, check_nulls, 1) != SPI_OK_SELECT)
elog(ERROR, "SPI_execp() failed in RI_FKey_check()"); elog(ERROR, "SPI_execp() failed in RI_FKey_check()");
SetUserId(save_uid);
if (SPI_processed == 0) if (SPI_processed == 0)
elog(ERROR, "%s referential integrity violation - " elog(ERROR, "%s referential integrity violation - "
"no rows found in %s", "no rows found in %s",
...@@ -435,9 +443,14 @@ RI_FKey_check(PG_FUNCTION_ARGS) ...@@ -435,9 +443,14 @@ RI_FKey_check(PG_FUNCTION_ARGS)
* Now check that foreign key exists in PK table * Now check that foreign key exists in PK table
* ---------- * ----------
*/ */
SetUserId(RelationGetForm(pk_rel)->relowner);
if (SPI_execp(qplan, check_values, check_nulls, 1) != SPI_OK_SELECT) if (SPI_execp(qplan, check_values, check_nulls, 1) != SPI_OK_SELECT)
elog(ERROR, "SPI_execp() failed in RI_FKey_check()"); elog(ERROR, "SPI_execp() failed in RI_FKey_check()");
SetUserId(save_uid);
if (SPI_processed == 0) if (SPI_processed == 0)
elog(ERROR, "%s referential integrity violation - " elog(ERROR, "%s referential integrity violation - "
"key referenced from %s not found in %s", "key referenced from %s not found in %s",
...@@ -508,6 +521,9 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS) ...@@ -508,6 +521,9 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS)
char del_nulls[RI_MAX_NUMKEYS + 1]; char del_nulls[RI_MAX_NUMKEYS + 1];
bool isnull; bool isnull;
int i; int i;
Oid save_uid;
save_uid = GetUserId();
ReferentialIntegritySnapshotOverride = true; ReferentialIntegritySnapshotOverride = true;
...@@ -659,9 +675,13 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS) ...@@ -659,9 +675,13 @@ RI_FKey_noaction_del(PG_FUNCTION_ARGS)
* Now check for existing references * Now check for existing references
* ---------- * ----------
*/ */
SetUserId(RelationGetForm(pk_rel)->relowner);
if (SPI_execp(qplan, del_values, del_nulls, 1) != SPI_OK_SELECT) if (SPI_execp(qplan, del_values, del_nulls, 1) != SPI_OK_SELECT)
elog(ERROR, "SPI_execp() failed in RI_FKey_noaction_del()"); elog(ERROR, "SPI_execp() failed in RI_FKey_noaction_del()");
SetUserId(save_uid);
if (SPI_processed > 0) if (SPI_processed > 0)
elog(ERROR, "%s referential integrity violation - " elog(ERROR, "%s referential integrity violation - "
"key in %s still referenced from %s", "key in %s still referenced from %s",
...@@ -716,6 +736,9 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS) ...@@ -716,6 +736,9 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS)
char upd_nulls[RI_MAX_NUMKEYS + 1]; char upd_nulls[RI_MAX_NUMKEYS + 1];
bool isnull; bool isnull;
int i; int i;
Oid save_uid;
save_uid = GetUserId();
ReferentialIntegritySnapshotOverride = true; ReferentialIntegritySnapshotOverride = true;
...@@ -876,9 +899,13 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS) ...@@ -876,9 +899,13 @@ RI_FKey_noaction_upd(PG_FUNCTION_ARGS)
* Now check for existing references * Now check for existing references
* ---------- * ----------
*/ */
SetUserId(RelationGetForm(pk_rel)->relowner);
if (SPI_execp(qplan, upd_values, upd_nulls, 1) != SPI_OK_SELECT) if (SPI_execp(qplan, upd_values, upd_nulls, 1) != SPI_OK_SELECT)
elog(ERROR, "SPI_execp() failed in RI_FKey_noaction_upd()"); elog(ERROR, "SPI_execp() failed in RI_FKey_noaction_upd()");
SetUserId(save_uid);
if (SPI_processed > 0) if (SPI_processed > 0)
elog(ERROR, "%s referential integrity violation - " elog(ERROR, "%s referential integrity violation - "
"key in %s still referenced from %s", "key in %s still referenced from %s",
...@@ -1570,6 +1597,9 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS) ...@@ -1570,6 +1597,9 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
char upd_nulls[RI_MAX_NUMKEYS + 1]; char upd_nulls[RI_MAX_NUMKEYS + 1];
bool isnull; bool isnull;
int i; int i;
Oid save_uid;
save_uid = GetUserId();
ReferentialIntegritySnapshotOverride = true; ReferentialIntegritySnapshotOverride = true;
...@@ -1730,9 +1760,13 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS) ...@@ -1730,9 +1760,13 @@ RI_FKey_restrict_upd(PG_FUNCTION_ARGS)
* Now check for existing references * Now check for existing references
* ---------- * ----------
*/ */
SetUserId(RelationGetForm(pk_rel)->relowner);
if (SPI_execp(qplan, upd_values, upd_nulls, 1) != SPI_OK_SELECT) if (SPI_execp(qplan, upd_values, upd_nulls, 1) != SPI_OK_SELECT)
elog(ERROR, "SPI_execp() failed in RI_FKey_restrict_upd()"); elog(ERROR, "SPI_execp() failed in RI_FKey_restrict_upd()");
SetUserId(save_uid);
if (SPI_processed > 0) if (SPI_processed > 0)
elog(ERROR, "%s referential integrity violation - " elog(ERROR, "%s referential integrity violation - "
"key in %s still referenced from %s", "key in %s still referenced from %s",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment