Skip to content

Commit 53c4f12

Browse files
committed
UPDATE ... SET <col> = DEFAULT
Rod Taylor
1 parent a09ccc7 commit 53c4f12

File tree

9 files changed

+90
-49
lines changed

9 files changed

+90
-49
lines changed

doc/src/sgml/ref/update.sgml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.21 2003/04/26 23:56:51 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.22 2003/06/25 04:19:24 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -16,7 +16,7 @@ PostgreSQL documentation
1616

1717
<refsynopsisdiv>
1818
<synopsis>
19-
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = <replaceable class="PARAMETER">expression</replaceable> [, ...]
19+
UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...]
2020
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
2121
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
2222
</synopsis>
@@ -77,6 +77,15 @@ UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replacea
7777
</listitem>
7878
</varlistentry>
7979

80+
<varlistentry>
81+
<term><literal>DEFAULT</literal></term>
82+
<listitem>
83+
<para>
84+
This column will be filled with its default value.
85+
</para>
86+
</listitem>
87+
</varlistentry>
88+
8089
<varlistentry>
8190
<term><replaceable class="PARAMETER">fromlist</replaceable></term>
8291
<listitem>

src/backend/nodes/copyfuncs.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.254 2003/06/25 03:40:17 momjian Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.255 2003/06/25 04:19:24 momjian Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1661,10 +1661,10 @@ _copyFuncWithArgs(FuncWithArgs *from)
16611661
return newnode;
16621662
}
16631663

1664-
static InsertDefault *
1665-
_copyInsertDefault(InsertDefault *from)
1664+
static SetToDefault *
1665+
_copySetToDefault(SetToDefault *from)
16661666
{
1667-
InsertDefault *newnode = makeNode(InsertDefault);
1667+
SetToDefault *newnode = makeNode(SetToDefault);
16681668

16691669
return newnode;
16701670
}
@@ -2942,8 +2942,8 @@ copyObject(void *from)
29422942
case T_FuncWithArgs:
29432943
retval = _copyFuncWithArgs(from);
29442944
break;
2945-
case T_InsertDefault:
2946-
retval = _copyInsertDefault(from);
2945+
case T_SetToDefault:
2946+
retval = _copySetToDefault(from);
29472947
break;
29482948

29492949
default:

src/backend/nodes/equalfuncs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.197 2003/06/25 03:40:17 momjian Exp $
21+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.198 2003/06/25 04:19:24 momjian Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -728,7 +728,7 @@ _equalFuncWithArgs(FuncWithArgs *a, FuncWithArgs *b)
728728
}
729729

730730
static bool
731-
_equalInsertDefault(InsertDefault *a, InsertDefault *b)
731+
_equalSetToDefault(SetToDefault *a, SetToDefault *b)
732732
{
733733
return true;
734734
}
@@ -2055,8 +2055,8 @@ equal(void *a, void *b)
20552055
case T_FuncWithArgs:
20562056
retval = _equalFuncWithArgs(a, b);
20572057
break;
2058-
case T_InsertDefault:
2059-
retval = _equalInsertDefault(a, b);
2058+
case T_SetToDefault:
2059+
retval = _equalSetToDefault(a, b);
20602060
break;
20612061

20622062
default:

src/backend/parser/analyze.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.276 2003/06/25 03:40:17 momjian Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.277 2003/06/25 04:19:24 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -660,23 +660,9 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
660660
col = (ResTarget *) lfirst(icolumns);
661661
Assert(IsA(col, ResTarget));
662662

663-
/*
664-
* When the value is to be set to the column default we can simply
665-
* drop the TLE now and handle it later on using methods for missing
666-
* columns.
667-
*/
668-
if (IsA(tle, InsertDefault))
669-
{
670-
qry->targetList = lremove(tle, qry->targetList);
671-
/* Note: the stmt->cols list is not adjusted to match */
672-
}
673-
else
674-
{
675-
/* Normal case */
676-
Assert(!tle->resdom->resjunk);
677-
updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
678-
col->indirection);
679-
}
663+
Assert(!tle->resdom->resjunk);
664+
updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
665+
col->indirection);
680666

681667
icolumns = lnext(icolumns);
682668
attnos = lnext(attnos);
@@ -2431,10 +2417,12 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
24312417
if (origTargetList == NIL)
24322418
elog(ERROR, "UPDATE target count mismatch --- internal error");
24332419
origTarget = (ResTarget *) lfirst(origTargetList);
2420+
24342421
updateTargetListEntry(pstate, tle, origTarget->name,
24352422
attnameAttNum(pstate->p_target_relation,
24362423
origTarget->name, true),
24372424
origTarget->indirection);
2425+
24382426
origTargetList = lnext(origTargetList);
24392427
}
24402428
if (origTargetList != NIL)

src/backend/parser/gram.y

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.419 2003/06/25 03:40:18 momjian Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.420 2003/06/25 04:19:24 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -6945,6 +6945,15 @@ update_target_el:
69456945
$$->indirection = $2;
69466946
$$->val = (Node *)$4;
69476947
}
6948+
| ColId opt_indirection '=' DEFAULT
6949+
{
6950+
SetToDefault *def = makeNode(SetToDefault);
6951+
$$ = makeNode(ResTarget);
6952+
$$->name = $1;
6953+
$$->indirection = NULL;
6954+
$$->val = (Node *)def;
6955+
}
6956+
69486957
;
69496958

69506959
insert_target_list:
@@ -6956,7 +6965,7 @@ insert_target_el:
69566965
target_el { $$ = $1; }
69576966
| DEFAULT
69586967
{
6959-
InsertDefault *def = makeNode(InsertDefault);
6968+
SetToDefault *def = makeNode(SetToDefault);
69606969
$$ = makeNode(ResTarget);
69616970
$$->name = NULL;
69626971
$$->indirection = NULL;

src/backend/parser/parse_target.c

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.102 2003/05/31 19:03:34 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.103 2003/06/25 04:19:24 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -177,19 +177,24 @@ transformTargetList(ParseState *pstate, List *targetlist)
177177
false));
178178
}
179179
}
180-
else if (IsA(res->val, InsertDefault))
180+
else if (IsA(res->val, SetToDefault))
181181
{
182-
InsertDefault *newnode = makeNode(InsertDefault);
183-
184182
/*
185-
* If this is a DEFAULT element, we make a junk entry which
186-
* will get dropped on return to transformInsertStmt().
183+
* If this is a DEFAULT element, we make a standard entry using
184+
* the default for the target expression. rewriteTargetList will
185+
* substitute the columns default for this expression.
187186
*/
188-
p_target = lappend(p_target, newnode);
187+
p_target = lappend(p_target,
188+
makeTargetEntry(makeResdom((AttrNumber) pstate->p_next_resno++,
189+
UNKNOWNOID,
190+
-1,
191+
res->name,
192+
false),
193+
(Expr *) res->val));
189194
}
190195
else
191196
{
192-
/* Everything else but ColumnRef and InsertDefault */
197+
/* Everything else but ColumnRef and SetToDefault */
193198
p_target = lappend(p_target,
194199
transformTargetEntry(pstate,
195200
res->val,
@@ -321,9 +326,10 @@ updateTargetListEntry(ParseState *pstate,
321326
int attrno,
322327
List *indirection)
323328
{
324-
Oid type_id = exprType((Node *) tle->expr); /* type of value provided */
329+
Oid type_id; /* type of value provided */
325330
Oid attrtype; /* type of target column */
326331
int32 attrtypmod;
332+
bool isDefault = false;
327333
Resdom *resnode = tle->resdom;
328334
Relation rd = pstate->p_target_relation;
329335

@@ -333,6 +339,17 @@ updateTargetListEntry(ParseState *pstate,
333339
attrtype = attnumTypeId(rd, attrno);
334340
attrtypmod = rd->rd_att->attrs[attrno - 1]->atttypmod;
335341

342+
/* The type of the default column is equivalent to that of the column */
343+
if (tle->expr != NULL && IsA(tle->expr, SetToDefault))
344+
{
345+
type_id = attrtype;
346+
isDefault = true;
347+
}
348+
349+
/* Otherwise the expression holds the type */
350+
else
351+
type_id = exprType((Node *) tle->expr);
352+
336353
/*
337354
* If there are subscripts on the target column, prepare an array
338355
* assignment expression. This will generate an array value that the
@@ -383,7 +400,7 @@ updateTargetListEntry(ParseState *pstate,
383400
* coercion. But accept InvalidOid, which indicates the source is
384401
* a NULL constant. (XXX is that still true?)
385402
*/
386-
if (type_id != InvalidOid)
403+
if (!isDefault && type_id != InvalidOid)
387404
{
388405
tle->expr = (Expr *)
389406
coerce_to_target_type(pstate,

src/backend/rewrite/rewriteHandler.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.120 2003/05/02 20:54:35 tgl Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.121 2003/06/25 04:19:24 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -307,7 +307,25 @@ rewriteTargetList(Query *parsetree, Relation target_relation)
307307
{
308308
Assert(strcmp(resdom->resname,
309309
NameStr(att_tup->attname)) == 0);
310-
new_tle = process_matched_tle(old_tle, new_tle);
310+
311+
if (old_tle->expr != NULL && IsA(old_tle->expr, SetToDefault))
312+
{
313+
/* Set to the default value of the column, as requested */
314+
Node *new_expr;
315+
316+
new_expr = build_column_default(target_relation, attrno);
317+
318+
new_tle = makeTargetEntry(makeResdom(attrno,
319+
att_tup->atttypid,
320+
att_tup->atttypmod,
321+
pstrdup(NameStr(att_tup->attname)),
322+
false),
323+
(Expr *) new_expr);
324+
}
325+
else
326+
/* Normal Case */
327+
new_tle = process_matched_tle(old_tle, new_tle);
328+
311329
/* keep scanning to detect multiple assignments to attr */
312330
}
313331
}

src/include/nodes/nodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: nodes.h,v 1.141 2003/06/25 03:40:19 momjian Exp $
10+
* $Id: nodes.h,v 1.142 2003/06/25 04:19:24 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -277,7 +277,7 @@ typedef enum NodeTag
277277
T_PrivGrantee,
278278
T_FuncWithArgs,
279279
T_PrivTarget,
280-
T_InsertDefault,
280+
T_SetToDefault,
281281
T_CreateOpClassItem,
282282
T_CompositeTypeStmt,
283283
T_InhRelation,

src/include/nodes/parsenodes.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.239 2003/06/25 03:40:19 momjian Exp $
10+
* $Id: parsenodes.h,v 1.240 2003/06/25 04:19:24 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -279,10 +279,10 @@ typedef struct ResTarget
279279
/*
280280
* Empty node used as a marker for Default Columns
281281
*/
282-
typedef struct InsertDefault
282+
typedef struct SetToDefault
283283
{
284284
NodeTag type;
285-
} InsertDefault;
285+
} SetToDefault;
286286

287287
/*
288288
* SortGroupBy - for ORDER BY clause

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