aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/copyfrom.c
diff options
context:
space:
mode:
authorAndrew Dunstan <andrew@dunslane.net>2023-03-13 10:01:56 -0400
committerAndrew Dunstan <andrew@dunslane.net>2023-03-13 10:01:56 -0400
commit9f8377f7a27910bf0f35bf5169a8046731948a79 (patch)
treee0a19449e4cebb8923dfcd1bba95a1d3363faa32 /src/backend/commands/copyfrom.c
parent7b14e20b12cc8358cad9bdd05dd6b7de7f73c431 (diff)
downloadpostgresql-9f8377f7a27910bf0f35bf5169a8046731948a79.tar.gz
postgresql-9f8377f7a27910bf0f35bf5169a8046731948a79.zip
Add a DEFAULT option to COPY FROM
This allows for a string which if an input field matches causes the column's default value to be inserted. The advantage of this is that the default can be inserted in some rows and not others, for which non-default data is available. The file_fdw extension is also modified to take allow use of this option. Israel Barth Rubio Discussion: https://postgr.es/m/CAO_rXXAcqesk6DsvioOZ5zmeEmpUN5ktZf-9=9yu+DTr0Xr8Uw@mail.gmail.com
Diffstat (limited to 'src/backend/commands/copyfrom.c')
-rw-r--r--src/backend/commands/copyfrom.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/backend/commands/copyfrom.c b/src/backend/commands/copyfrom.c
index 29cd1cf4a63..321a7fad854 100644
--- a/src/backend/commands/copyfrom.c
+++ b/src/backend/commands/copyfrom.c
@@ -1565,11 +1565,11 @@ BeginCopyFrom(ParseState *pstate,
&in_func_oid, &typioparams[attnum - 1]);
fmgr_info(in_func_oid, &in_functions[attnum - 1]);
- /* Get default info if needed */
- if (!list_member_int(cstate->attnumlist, attnum) && !att->attgenerated)
+ /* Get default info if available */
+ defexprs[attnum - 1] = NULL;
+
+ if (!att->attgenerated)
{
- /* attribute is NOT to be copied from input */
- /* use default value if one exists */
Expr *defexpr = (Expr *) build_column_default(cstate->rel,
attnum);
@@ -1579,9 +1579,15 @@ BeginCopyFrom(ParseState *pstate,
defexpr = expression_planner(defexpr);
/* Initialize executable expression in copycontext */
- defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
- defmap[num_defaults] = attnum - 1;
- num_defaults++;
+ defexprs[attnum - 1] = ExecInitExpr(defexpr, NULL);
+
+ /* if NOT copied from input */
+ /* use default value if one exists */
+ if (!list_member_int(cstate->attnumlist, attnum))
+ {
+ defmap[num_defaults] = attnum - 1;
+ num_defaults++;
+ }
/*
* If a default expression looks at the table being loaded,