<!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.21 2003/04/26 23:56:51 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/update.sgml,v 1.22 2003/06/25 04:19:24 momjian Exp $
PostgreSQL documentation
-->
<refsynopsisdiv>
<synopsis>
-UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = <replaceable class="PARAMETER">expression</replaceable> [, ...]
+UPDATE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> SET <replaceable class="PARAMETER">column</replaceable> = { <replaceable class="PARAMETER">expression</replaceable> | DEFAULT } [, ...]
[ FROM <replaceable class="PARAMETER">fromlist</replaceable> ]
[ WHERE <replaceable class="PARAMETER">condition</replaceable> ]
</synopsis>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term><literal>DEFAULT</literal></term>
+ <listitem>
+ <para>
+ This column will be filled with its default value.
+ </para>
+ </listitem>
+ </varlistentry>
+
<varlistentry>
<term><replaceable class="PARAMETER">fromlist</replaceable></term>
<listitem>
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.254 2003/06/25 03:40:17 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.255 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
return newnode;
}
-static InsertDefault *
-_copyInsertDefault(InsertDefault *from)
+static SetToDefault *
+_copySetToDefault(SetToDefault *from)
{
- InsertDefault *newnode = makeNode(InsertDefault);
+ SetToDefault *newnode = makeNode(SetToDefault);
return newnode;
}
case T_FuncWithArgs:
retval = _copyFuncWithArgs(from);
break;
- case T_InsertDefault:
- retval = _copyInsertDefault(from);
+ case T_SetToDefault:
+ retval = _copySetToDefault(from);
break;
default:
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.197 2003/06/25 03:40:17 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.198 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
}
static bool
-_equalInsertDefault(InsertDefault *a, InsertDefault *b)
+_equalSetToDefault(SetToDefault *a, SetToDefault *b)
{
return true;
}
case T_FuncWithArgs:
retval = _equalFuncWithArgs(a, b);
break;
- case T_InsertDefault:
- retval = _equalInsertDefault(a, b);
+ case T_SetToDefault:
+ retval = _equalSetToDefault(a, b);
break;
default:
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.276 2003/06/25 03:40:17 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/analyze.c,v 1.277 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
col = (ResTarget *) lfirst(icolumns);
Assert(IsA(col, ResTarget));
- /*
- * When the value is to be set to the column default we can simply
- * drop the TLE now and handle it later on using methods for missing
- * columns.
- */
- if (IsA(tle, InsertDefault))
- {
- qry->targetList = lremove(tle, qry->targetList);
- /* Note: the stmt->cols list is not adjusted to match */
- }
- else
- {
- /* Normal case */
- Assert(!tle->resdom->resjunk);
- updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
- col->indirection);
- }
+ Assert(!tle->resdom->resjunk);
+ updateTargetListEntry(pstate, tle, col->name, lfirsti(attnos),
+ col->indirection);
icolumns = lnext(icolumns);
attnos = lnext(attnos);
if (origTargetList == NIL)
elog(ERROR, "UPDATE target count mismatch --- internal error");
origTarget = (ResTarget *) lfirst(origTargetList);
+
updateTargetListEntry(pstate, tle, origTarget->name,
attnameAttNum(pstate->p_target_relation,
origTarget->name, true),
origTarget->indirection);
+
origTargetList = lnext(origTargetList);
}
if (origTargetList != NIL)
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.419 2003/06/25 03:40:18 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.420 2003/06/25 04:19:24 momjian Exp $
*
* HISTORY
* AUTHOR DATE MAJOR EVENT
$$->indirection = $2;
$$->val = (Node *)$4;
}
+ | ColId opt_indirection '=' DEFAULT
+ {
+ SetToDefault *def = makeNode(SetToDefault);
+ $$ = makeNode(ResTarget);
+ $$->name = $1;
+ $$->indirection = NULL;
+ $$->val = (Node *)def;
+ }
+
;
insert_target_list:
target_el { $$ = $1; }
| DEFAULT
{
- InsertDefault *def = makeNode(InsertDefault);
+ SetToDefault *def = makeNode(SetToDefault);
$$ = makeNode(ResTarget);
$$->name = NULL;
$$->indirection = NULL;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.102 2003/05/31 19:03:34 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/parser/parse_target.c,v 1.103 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
false));
}
}
- else if (IsA(res->val, InsertDefault))
+ else if (IsA(res->val, SetToDefault))
{
- InsertDefault *newnode = makeNode(InsertDefault);
-
/*
- * If this is a DEFAULT element, we make a junk entry which
- * will get dropped on return to transformInsertStmt().
+ * If this is a DEFAULT element, we make a standard entry using
+ * the default for the target expression. rewriteTargetList will
+ * substitute the columns default for this expression.
*/
- p_target = lappend(p_target, newnode);
+ p_target = lappend(p_target,
+ makeTargetEntry(makeResdom((AttrNumber) pstate->p_next_resno++,
+ UNKNOWNOID,
+ -1,
+ res->name,
+ false),
+ (Expr *) res->val));
}
else
{
- /* Everything else but ColumnRef and InsertDefault */
+ /* Everything else but ColumnRef and SetToDefault */
p_target = lappend(p_target,
transformTargetEntry(pstate,
res->val,
int attrno,
List *indirection)
{
- Oid type_id = exprType((Node *) tle->expr); /* type of value provided */
+ Oid type_id; /* type of value provided */
Oid attrtype; /* type of target column */
int32 attrtypmod;
+ bool isDefault = false;
Resdom *resnode = tle->resdom;
Relation rd = pstate->p_target_relation;
attrtype = attnumTypeId(rd, attrno);
attrtypmod = rd->rd_att->attrs[attrno - 1]->atttypmod;
+ /* The type of the default column is equivalent to that of the column */
+ if (tle->expr != NULL && IsA(tle->expr, SetToDefault))
+ {
+ type_id = attrtype;
+ isDefault = true;
+ }
+
+ /* Otherwise the expression holds the type */
+ else
+ type_id = exprType((Node *) tle->expr);
+
/*
* If there are subscripts on the target column, prepare an array
* assignment expression. This will generate an array value that the
* coercion. But accept InvalidOid, which indicates the source is
* a NULL constant. (XXX is that still true?)
*/
- if (type_id != InvalidOid)
+ if (!isDefault && type_id != InvalidOid)
{
tle->expr = (Expr *)
coerce_to_target_type(pstate,
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.120 2003/05/02 20:54:35 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.121 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
{
Assert(strcmp(resdom->resname,
NameStr(att_tup->attname)) == 0);
- new_tle = process_matched_tle(old_tle, new_tle);
+
+ if (old_tle->expr != NULL && IsA(old_tle->expr, SetToDefault))
+ {
+ /* Set to the default value of the column, as requested */
+ Node *new_expr;
+
+ new_expr = build_column_default(target_relation, attrno);
+
+ new_tle = makeTargetEntry(makeResdom(attrno,
+ att_tup->atttypid,
+ att_tup->atttypmod,
+ pstrdup(NameStr(att_tup->attname)),
+ false),
+ (Expr *) new_expr);
+ }
+ else
+ /* Normal Case */
+ new_tle = process_matched_tle(old_tle, new_tle);
+
/* keep scanning to detect multiple assignments to attr */
}
}
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: nodes.h,v 1.141 2003/06/25 03:40:19 momjian Exp $
+ * $Id: nodes.h,v 1.142 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
T_PrivGrantee,
T_FuncWithArgs,
T_PrivTarget,
- T_InsertDefault,
+ T_SetToDefault,
T_CreateOpClassItem,
T_CompositeTypeStmt,
T_InhRelation,
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: parsenodes.h,v 1.239 2003/06/25 03:40:19 momjian Exp $
+ * $Id: parsenodes.h,v 1.240 2003/06/25 04:19:24 momjian Exp $
*
*-------------------------------------------------------------------------
*/
/*
* Empty node used as a marker for Default Columns
*/
-typedef struct InsertDefault
+typedef struct SetToDefault
{
NodeTag type;
-} InsertDefault;
+} SetToDefault;
/*
* SortGroupBy - for ORDER BY clause