There are currently a bunch of items on the Todo relating to SQL
INSERT statements, one of which is allowing INSERTs of the following
form:

INSERT INTO tab [(c1, c2, ...)] VALUES (x1, y1, ...), (x2, y2, ...), ...

I had written a quick 'n dirty patch that accomplished this by
splitting an INSERT of multiple tuples into multiple individual
INSERT statements. However, this approach was not the right way to
go, and so I would like to propose the following as a plan for
getting implementing the feature:

Coming out of the parser (modified to handle the new syntax), an
InsertStmt variable will hold a list of lists of ResTargets. These
lists of ResTargets will be transformed one by one into lists of
TargetEntry's. The transformed lists would be kept in the Query node
that represents the INSERT statement in a new structure member.

With the Result node, PostgreSQL currently has support in the executor
for retrieving a single tuple that is not stored in any base
relation. When doing an INSERT of a single tuple via VALUES, the
current scheme suffices. However, for inserting multiple tuples via
INSERT, the use of the Result node doesn't work, AFAICS. While it
should be possible to modify the Result node to handle multiple
tuples, I would rather not use Result as Result is also used for
constant qualifications. It seems cleaner to have a seperate node
structure that deals with tuples that have no real base relation;
there would be no need to deal with extra cruft in Result-related
functions.

I would like to add a new executor node called Values (suggestions for a
better name are welcome -- there is already a Value node) that would
replace the use of Result for insert statements. A tuple would just be a
List of TargetEntry's (like what Result currently does). The Values node
would just keep a List of those and when asked for a tuple, would return
the next element on the List. It would return NULL when done. The planner
would need to know when to create one -- that can be done query_planner()
as a call to a make_values() function if query->valuesList is not null.

Also, at some point, it would be nice to put together a new statement
node type that represents the SQL <query expression>. This node would
be used everywhere that a <query expression> could be used, hiding
the complexity of having to deal with either a SELECT statement or a
VALUES clause. For example, the parser rule for INSERT statements
would be simplified as well as the transformInsertStmt function. I
haven't thought about this much though. I would like to get multiple
insertion working first before looking at simplification.

Comments?

Liam

-- 
Liam Stewart :: Red Hat Canada, Ltd. :: liams@redhat.com

Mon, 13 Aug 2001 16:03:04 -0400
Liam Stewart <liams@redhat.com>
RFC: Inserting multiple values via INSERT ... VALUES ...
Liam Stewart <liams@redhat.com> writes:
> While it
> should be possible to modify the Result node to handle multiple
> tuples, I would rather not use Result as Result is also used for
> constant qualifications.

If you don't want to extend Result, I'd suggest generating a plan that
is an Append of Result nodes.  There's no need to add a new node type.

Personally I wouldn't have a problem with allowing Result to handle
a list of targetlists, though.  It's already capable of returning
multiple rows (when there's a function-returning-set in the targetlist)
so I don't find multiple targetlists to be that much of an uglification.

> Also, at some point, it would be nice to put together a new statement
> node type that represents the SQL <query expression>. This node would
> be used everywhere that a <query expression> could be used, hiding
> the complexity of having to deal with either a SELECT statement or a
> VALUES clause. For example, the parser rule for INSERT statements
> would be simplified as well as the transformInsertStmt function.

The present handling of INSERT/SELECT is really really ugly.  The whole
querytree structure desperately needs to be rethought, actually.  You
can find some theorizing about what to do in the mail list archives,
but we're still far from having a detailed plan.

			regards, tom lane

Tue, 14 Aug 2001 13:39:10 -0400
Tom Lane <tgl@sss.pgh.pa.us>
Re: RFC: Inserting multiple values via INSERT ... VALUES ... 