Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1bd159e

Browse files
committed
Fix bogus coding of SET DEFAULT ri triggers ... or at least make it less
bogus than it was. Per bug report from Adrian Pop.
1 parent15b9e2c commit1bd159e

File tree

1 file changed

+46
-83
lines changed

1 file changed

+46
-83
lines changed

‎src/backend/utils/adt/ri_triggers.c

Lines changed: 46 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1919
*
20-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.47 2003/03/15 21:19:40 tgl Exp $
20+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ri_triggers.c,v 1.48 2003/03/27 19:25:40 tgl Exp $
2121
*
2222
* ----------
2323
*/
@@ -37,6 +37,7 @@
3737
#include"executor/spi_priv.h"
3838
#include"optimizer/planmain.h"
3939
#include"parser/parse_oper.h"
40+
#include"rewrite/rewriteHandler.h"
4041
#include"utils/lsyscache.h"
4142
#include"miscadmin.h"
4243

@@ -2315,10 +2316,8 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
23152316
constchar*qualsep;
23162317
Oidqueryoids[RI_MAX_NUMKEYS];
23172318
Plan*spi_plan;
2318-
AttrDefault*defval;
2319-
TargetEntry*spi_qptle;
2320-
inti,
2321-
j;
2319+
inti;
2320+
List*l;
23222321

23232322
/* ----------
23242323
* The query string built is
@@ -2355,45 +2354,31 @@ RI_FKey_setdefault_del(PG_FUNCTION_ARGS)
23552354
*/
23562355
qplan=SPI_prepare(querystr,qkey.nkeypairs,queryoids);
23572356

2358-
/* ----------
2359-
* Here now follows very ugly code depending on internals
2360-
* of the SPI manager.
2361-
*
2362-
* EVIL EVIL EVIL (but must be - Jan)
2357+
/*
2358+
* Scan the plan's targetlist and replace the NULLs by
2359+
* appropriate column defaults, if any (if not, they stay
2360+
* NULL).
23632361
*
2364-
* We replace the CONST NULL targetlist expressions
2365-
* in the generated plan by (any) default values found
2366-
* in the tuple constructor.
2367-
* ----------
2362+
* XXX This is really ugly; it'd be better to use "UPDATE
2363+
* SET foo = DEFAULT", if we had it.
23682364
*/
23692365
spi_plan= (Plan*)lfirst(((_SPI_plan*)qplan)->ptlist);
2370-
if (fk_rel->rd_att->constr!=NULL)
2371-
defval=fk_rel->rd_att->constr->defval;
2372-
else
2373-
defval=NULL;
2374-
for (i=0;i<qkey.nkeypairs&&defval!=NULL;i++)
2366+
foreach(l,spi_plan->targetlist)
23752367
{
2376-
/*
2377-
* For each key attribute lookup the tuple constructor
2378-
* for a corresponding default value
2379-
*/
2380-
for (j=0;j<fk_rel->rd_att->constr->num_defval;j++)
2368+
TargetEntry*tle= (TargetEntry*)lfirst(l);
2369+
Node*dfl;
2370+
2371+
/* Ignore any junk columns or Var=Var columns */
2372+
if (tle->resdom->resjunk)
2373+
continue;
2374+
if (IsA(tle->expr,Var))
2375+
continue;
2376+
2377+
dfl=build_column_default(fk_rel,tle->resdom->resno);
2378+
if (dfl)
23812379
{
2382-
if (defval[j].adnum==
2383-
qkey.keypair[i][RI_KEYPAIR_FK_IDX])
2384-
{
2385-
/*
2386-
* That's the one - push the expression from
2387-
* defval.adbin into the plan's targetlist
2388-
*/
2389-
spi_qptle= (TargetEntry*)
2390-
nth(defval[j].adnum-1,
2391-
spi_plan->targetlist);
2392-
spi_qptle->expr=stringToNode(defval[j].adbin);
2393-
fix_opfuncids((Node*)spi_qptle->expr);
2394-
2395-
break;
2396-
}
2380+
fix_opfuncids(dfl);
2381+
tle->expr= (Expr*)dfl;
23972382
}
23982383
}
23992384
}
@@ -2559,10 +2544,8 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
25592544
constchar*qualsep;
25602545
Oidqueryoids[RI_MAX_NUMKEYS];
25612546
Plan*spi_plan;
2562-
AttrDefault*defval;
2563-
TargetEntry*spi_qptle;
2564-
inti,
2565-
j;
2547+
inti;
2548+
List*l;
25662549

25672550
/* ----------
25682551
* The query string built is
@@ -2610,50 +2593,30 @@ RI_FKey_setdefault_upd(PG_FUNCTION_ARGS)
26102593
qplan=SPI_prepare(querystr,qkey.nkeypairs,queryoids);
26112594

26122595
/*
2613-
* Now replace the CONST NULL targetlist expressions in
2614-
* the generated plan by (any) default values found in the
2615-
* tuple constructor.
2596+
* Scan the plan's targetlist and replace the NULLs by
2597+
* appropriate column defaults, if any (if not, they stay
2598+
* NULL).
2599+
*
2600+
* XXX This is really ugly; it'd be better to use "UPDATE
2601+
* SET foo = DEFAULT", if we had it.
26162602
*/
26172603
spi_plan= (Plan*)lfirst(((_SPI_plan*)qplan)->ptlist);
2618-
if (fk_rel->rd_att->constr!=NULL)
2619-
defval=fk_rel->rd_att->constr->defval;
2620-
else
2621-
defval=NULL;
2622-
for (i=0;i<qkey.nkeypairs&&defval!=NULL;i++)
2604+
foreach(l,spi_plan->targetlist)
26232605
{
2624-
/*
2625-
* MATCH <unspecified> - only change columns
2626-
* corresponding to changed columns in pk_rel's key.
2627-
* This conditional must match the one in the loop
2628-
* above that built the SET attrn=NULL list.
2629-
*/
2630-
if (match_type==RI_MATCH_TYPE_FULL||
2631-
!ri_OneKeyEqual(pk_rel,i,old_row,
2632-
new_row,&qkey,RI_KEYPAIR_PK_IDX))
2606+
TargetEntry*tle= (TargetEntry*)lfirst(l);
2607+
Node*dfl;
2608+
2609+
/* Ignore any junk columns or Var=Var columns */
2610+
if (tle->resdom->resjunk)
2611+
continue;
2612+
if (IsA(tle->expr,Var))
2613+
continue;
2614+
2615+
dfl=build_column_default(fk_rel,tle->resdom->resno);
2616+
if (dfl)
26332617
{
2634-
/*
2635-
* For each key attribute lookup the tuple
2636-
* constructor for a corresponding default value
2637-
*/
2638-
for (j=0;j<fk_rel->rd_att->constr->num_defval;j++)
2639-
{
2640-
if (defval[j].adnum==
2641-
qkey.keypair[i][RI_KEYPAIR_FK_IDX])
2642-
{
2643-
/*
2644-
* That's the one - push the expression
2645-
* from defval.adbin into the plan's
2646-
* targetlist
2647-
*/
2648-
spi_qptle= (TargetEntry*)
2649-
nth(defval[j].adnum-1,
2650-
spi_plan->targetlist);
2651-
spi_qptle->expr=stringToNode(defval[j].adbin);
2652-
fix_opfuncids((Node*)spi_qptle->expr);
2653-
2654-
break;
2655-
}
2656-
}
2618+
fix_opfuncids(dfl);
2619+
tle->expr= (Expr*)dfl;
26572620
}
26582621
}
26592622
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp