Skip to content

Commit a15207f

Browse files
committed
Now that we have UPDATE tab SET col = DEFAULT, get rid of horrid hack
in the RI triggers for ON DELETE/UPDATE SET DEFAULT. The code depended way too much on knowledge of plan structure, and yet still would fail if the generated query got rewritten by rules.
1 parent 3900f83 commit a15207f

File tree

1 file changed

+5
-65
lines changed

1 file changed

+5
-65
lines changed

src/backend/utils/adt/ri_triggers.c

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
1919
*
20-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.58 2003/09/25 18:58:35 tgl Exp $
20+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.59 2003/09/28 02:11:23 tgl Exp $
2121
*
2222
* ----------
2323
*/
@@ -2144,13 +2144,11 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
21442144
const char *querysep;
21452145
const char *qualsep;
21462146
Oid queryoids[RI_MAX_NUMKEYS];
2147-
Plan *spi_plan;
21482147
int i;
2149-
List *l;
21502148

21512149
/* ----------
21522150
* The query string built is
2153-
* UPDATE ONLY <fktable> SET fkatt1 = NULL [, ...]
2151+
* UPDATE ONLY <fktable> SET fkatt1 = DEFAULT [, ...]
21542152
* WHERE fkatt1 = $1 [AND ...]
21552153
* The type id's for the $ parameters are those of the
21562154
* corresponding PK attributes. Thus, ri_PlanCheck could
@@ -2167,7 +2165,7 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
21672165
{
21682166
quoteOneName(attname,
21692167
tgargs[RI_FIRST_ATTNAME_ARGNO + i * 2 + RI_KEYPAIR_FK_IDX]);
2170-
snprintf(querystr + strlen(querystr), sizeof(querystr) - strlen(querystr), "%s %s = NULL",
2168+
snprintf(querystr + strlen(querystr), sizeof(querystr) - strlen(querystr), "%s %s = DEFAULT",
21712169
querysep, attname);
21722170
snprintf(qualstr + strlen(qualstr), sizeof(qualstr) - strlen(qualstr), " %s %s = $%d",
21732171
qualsep, attname, i + 1);
@@ -2181,34 +2179,6 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
21812179
/* Prepare the plan, don't save it */
21822180
qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
21832181
&qkey, fk_rel, pk_rel, false);
2184-
2185-
/*
2186-
* Scan the plan's targetlist and replace the NULLs by
2187-
* appropriate column defaults, if any (if not, they stay
2188-
* NULL).
2189-
*
2190-
* XXX This is really ugly; it'd be better to use "UPDATE
2191-
* SET foo = DEFAULT", if we had it.
2192-
*/
2193-
spi_plan = (Plan *) lfirst(((_SPI_plan *) qplan)->ptlist);
2194-
foreach(l, spi_plan->targetlist)
2195-
{
2196-
TargetEntry *tle = (TargetEntry *) lfirst(l);
2197-
Node *dfl;
2198-
2199-
/* Ignore any junk columns or Var=Var columns */
2200-
if (tle->resdom->resjunk)
2201-
continue;
2202-
if (IsA(tle->expr, Var))
2203-
continue;
2204-
2205-
dfl = build_column_default(fk_rel, tle->resdom->resno);
2206-
if (dfl)
2207-
{
2208-
fix_opfuncids(dfl);
2209-
tle->expr = (Expr *) dfl;
2210-
}
2211-
}
22122182
}
22132183

22142184
/*
@@ -2368,13 +2338,11 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
23682338
const char *querysep;
23692339
const char *qualsep;
23702340
Oid queryoids[RI_MAX_NUMKEYS];
2371-
Plan *spi_plan;
23722341
int i;
2373-
List *l;
23742342

23752343
/* ----------
23762344
* The query string built is
2377-
* UPDATE ONLY <fktable> SET fkatt1 = NULL [, ...]
2345+
* UPDATE ONLY <fktable> SET fkatt1 = DEFAULT [, ...]
23782346
* WHERE fkatt1 = $1 [AND ...]
23792347
* The type id's for the $ parameters are those of the
23802348
* corresponding PK attributes. Thus, ri_PlanCheck could
@@ -2400,7 +2368,7 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
24002368
!ri_OneKeyEqual(pk_rel, i, old_row,
24012369
new_row, &qkey, RI_KEYPAIR_PK_IDX))
24022370
{
2403-
snprintf(querystr + strlen(querystr), sizeof(querystr) - strlen(querystr), "%s %s = NULL",
2371+
snprintf(querystr + strlen(querystr), sizeof(querystr) - strlen(querystr), "%s %s = DEFAULT",
24042372
querysep, attname);
24052373
querysep = ",";
24062374
}
@@ -2415,34 +2383,6 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
24152383
/* Prepare the plan, don't save it */
24162384
qplan = ri_PlanCheck(querystr, qkey.nkeypairs, queryoids,
24172385
&qkey, fk_rel, pk_rel, false);
2418-
2419-
/*
2420-
* Scan the plan's targetlist and replace the NULLs by
2421-
* appropriate column defaults, if any (if not, they stay
2422-
* NULL).
2423-
*
2424-
* XXX This is really ugly; it'd be better to use "UPDATE
2425-
* SET foo = DEFAULT", if we had it.
2426-
*/
2427-
spi_plan = (Plan *) lfirst(((_SPI_plan *) qplan)->ptlist);
2428-
foreach(l, spi_plan->targetlist)
2429-
{
2430-
TargetEntry *tle = (TargetEntry *) lfirst(l);
2431-
Node *dfl;
2432-
2433-
/* Ignore any junk columns or Var=Var columns */
2434-
if (tle->resdom->resjunk)
2435-
continue;
2436-
if (IsA(tle->expr, Var))
2437-
continue;
2438-
2439-
dfl = build_column_default(fk_rel, tle->resdom->resno);
2440-
if (dfl)
2441-
{
2442-
fix_opfuncids(dfl);
2443-
tle->expr = (Expr *) dfl;
2444-
}
2445-
}
24462386
}
24472387

24482388
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy