Skip to content

Commit c47b408

Browse files
committed
Fix ruleutils to print "INSERT INTO foo DEFAULT VALUES" correctly.
Per bug #7615 from Marko Tiikkaja. Apparently nobody ever tried this case before ...
1 parent 95ff5b3 commit c47b408

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,8 +3196,8 @@ get_insert_query_def(Query *query, deparse_context *context)
31963196
List *strippedexprs;
31973197

31983198
/*
3199-
* If it's an INSERT ... SELECT or VALUES (...), (...), ... there will be
3200-
* a single RTE for the SELECT or VALUES.
3199+
* If it's an INSERT ... SELECT or multi-row VALUES, there will be a
3200+
* single RTE for the SELECT or VALUES. Plain VALUES has neither.
32013201
*/
32023202
foreach(l, query->rtable)
32033203
{
@@ -3231,7 +3231,7 @@ get_insert_query_def(Query *query, deparse_context *context)
32313231
context->indentLevel += PRETTYINDENT_STD;
32323232
appendStringInfoChar(buf, ' ');
32333233
}
3234-
appendStringInfo(buf, "INSERT INTO %s (",
3234+
appendStringInfo(buf, "INSERT INTO %s ",
32353235
generate_relation_name(rte->relid, NIL));
32363236

32373237
/*
@@ -3248,6 +3248,8 @@ get_insert_query_def(Query *query, deparse_context *context)
32483248
values_cell = NULL;
32493249
strippedexprs = NIL;
32503250
sep = "";
3251+
if (query->targetList)
3252+
appendStringInfoChar(buf, '(');
32513253
foreach(l, query->targetList)
32523254
{
32533255
TargetEntry *tle = (TargetEntry *) lfirst(l);
@@ -3284,7 +3286,8 @@ get_insert_query_def(Query *query, deparse_context *context)
32843286
context, true));
32853287
}
32863288
}
3287-
appendStringInfo(buf, ") ");
3289+
if (query->targetList)
3290+
appendStringInfo(buf, ") ");
32883291

32893292
if (select_rte)
32903293
{
@@ -3299,7 +3302,7 @@ get_insert_query_def(Query *query, deparse_context *context)
32993302
/* Add the multi-VALUES expression lists */
33003303
get_values_def(values_rte->values_lists, context);
33013304
}
3302-
else
3305+
else if (strippedexprs)
33033306
{
33043307
/* A WITH clause is possible here */
33053308
get_with_clause(query, context);
@@ -3309,6 +3312,11 @@ get_insert_query_def(Query *query, deparse_context *context)
33093312
get_rule_expr((Node *) strippedexprs, context, false);
33103313
appendStringInfoChar(buf, ')');
33113314
}
3315+
else
3316+
{
3317+
/* No expressions, so it must be DEFAULT VALUES */
3318+
appendStringInfo(buf, "DEFAULT VALUES");
3319+
}
33123320

33133321
/* Add RETURNING if present */
33143322
if (query->returningList)

0 commit comments

Comments
 (0)