1999-07-22 17:09:15 +02:00
|
|
|
<!--
|
2003-05-04 04:23:16 +02:00
|
|
|
$Header: /cvsroot/pgsql/doc/src/sgml/ref/declare.sgml,v 1.24 2003/05/04 02:23:16 petere Exp $
|
2001-12-08 04:24:40 +01:00
|
|
|
PostgreSQL documentation
|
1999-07-22 17:09:15 +02:00
|
|
|
-->
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refentry id="SQL-DECLARE">
|
|
|
|
<refmeta>
|
2002-04-21 21:02:39 +02:00
|
|
|
<refentrytitle id="SQL-DECLARE-TITLE">DECLARE</refentrytitle>
|
1999-07-06 19:16:42 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
2003-05-04 04:23:16 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refnamediv>
|
2003-05-04 04:23:16 +02:00
|
|
|
<refname>DECLARE</refname>
|
|
|
|
<refpurpose>define a cursor</refpurpose>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refnamediv>
|
2003-05-04 04:23:16 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<refsynopsisdiv>
|
2003-05-04 04:23:16 +02:00
|
|
|
<synopsis>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
DECLARE <replaceable class="parameter">cursorname</replaceable> [ BINARY ] [ INSENSITIVE ] [ [ NO ] SCROLL ]
|
|
|
|
CURSOR [ { WITH | WITHOUT } HOLD ] FOR <replaceable class="parameter">query</replaceable>
|
2003-05-04 04:23:16 +02:00
|
|
|
[ FOR { READ ONLY | UPDATE [ OF <replaceable class="parameter">column</replaceable> [, ...] ] } ]
|
|
|
|
</synopsis>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refsynopsisdiv>
|
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
1999-07-22 17:09:15 +02:00
|
|
|
<command>DECLARE</command> allows a user to create cursors, which
|
|
|
|
can be used to retrieve
|
|
|
|
a small number of rows at a time out of a larger query. Cursors can
|
|
|
|
return data either in text or in binary format using
|
2000-12-26 00:15:27 +01:00
|
|
|
<xref linkend="sql-fetch" endterm="sql-fetch-title">.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
Normal cursors return data in text format, the same as a
|
|
|
|
<command>SELECT</> would produce. Since data is stored natively in
|
|
|
|
binary format, the system must do a conversion to produce the text
|
2003-05-04 04:23:16 +02:00
|
|
|
format. Once the information comes back in text form, the client
|
|
|
|
application may need to convert it to a binary format to manipulate
|
|
|
|
it. In addition, data in the text format is often larger in size
|
|
|
|
than in the binary format. Binary cursors return the data in the
|
|
|
|
native binary representation. Nevertheless, if you intend to
|
|
|
|
display the data as text anyway, retrieving it in text form will
|
|
|
|
save you some effort on the client side.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
1998-12-29 03:24:47 +01:00
|
|
|
<para>
|
1998-09-07 17:56:20 +02:00
|
|
|
As an example, if a query returns a value of one from an integer column,
|
2001-09-13 17:55:24 +02:00
|
|
|
you would get a string of <literal>1</> with a default cursor
|
1999-07-06 19:16:42 +02:00
|
|
|
whereas with a binary cursor you would get
|
2003-05-04 04:23:16 +02:00
|
|
|
a 4-byte value containing the internal representation of the value.
|
1999-07-22 17:09:15 +02:00
|
|
|
</para>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
1999-07-22 17:09:15 +02:00
|
|
|
<para>
|
2003-05-04 04:23:16 +02:00
|
|
|
Binary cursors should be used carefully. Many applications,
|
|
|
|
including <application>psql</application>, are not prepared to
|
|
|
|
handle binary cursors and expect data to come back in the text
|
|
|
|
format.
|
1998-12-29 03:24:47 +01:00
|
|
|
</para>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
|
|
|
<para>
|
2003-05-04 04:23:16 +02:00
|
|
|
The string representation is architecture-neutral whereas binary
|
2001-02-15 22:11:17 +01:00
|
|
|
representation can differ between different machine architectures.
|
2001-12-08 04:24:40 +01:00
|
|
|
<emphasis><productname>PostgreSQL</productname> does not resolve
|
2003-05-04 04:23:16 +02:00
|
|
|
byte ordering or representation issues for binary
|
|
|
|
cursors.</emphasis> Therefore, if your client machine and server
|
|
|
|
machine use different representations (e.g.,
|
|
|
|
<quote>big-endian</quote> versus <quote>little-endian</quote>), you
|
|
|
|
will probably not want your data returned in binary format.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">cursorname</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name of the cursor to be used in subsequent
|
|
|
|
<command>FETCH</command> operations.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>BINARY</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Causes the cursor to return data in binary rather than in text format.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>INSENSITIVE</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Indicates that data retrieved from the cursor should be
|
|
|
|
unaffected by updates to the tables underlying the cursor while
|
|
|
|
the cursor exists. In PostgreSQL, all cursors are insensitive;
|
|
|
|
this key word currently has no effect and is present for
|
|
|
|
compatibility with the SQL standard.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>SCROLL</literal></term>
|
|
|
|
<term><literal>NO SCROLL</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>SCROLL</literal> specifies that the cursor may be used
|
|
|
|
to retrieve rows in a nonsequential fashion (e.g.,
|
|
|
|
backward). Depending upon the complexity of the query's
|
|
|
|
execution plan, specifying <literal>SCROLL</literal> may impose
|
|
|
|
a performance penalty on the query's execution time.
|
|
|
|
<literal>NO SCROLL</literal> specifies that the cursor cannot be
|
|
|
|
used to retrieve rows in a nonsequential fashion.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>WITH HOLD</literal></term>
|
|
|
|
<term><literal>WITHOUT HOLD</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>WITH HOLD</literal> specifies that the cursor may
|
|
|
|
continue to be used after the transaction that created it
|
|
|
|
successfully commits. <literal>WITHOUT HOLD</literal> specifies
|
|
|
|
that the cursor cannot be used outside of the transaction that
|
|
|
|
created it. If neither <literal>WITHOUT HOLD</literal> nor
|
|
|
|
<literal>WITH HOLD</literal> is specified, <literal>WITHOUT
|
|
|
|
HOLD</literal> is the default.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">query</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
A <command>SELECT</> command that will provide the rows to be
|
|
|
|
returned by the cursor. Refer to <xref linkend="sql-select"
|
|
|
|
endterm="sql-select-title"> for further information about valid
|
|
|
|
queries.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><literal>FOR READ ONLY</literal></term>
|
|
|
|
<term><literal>FOR UPDATE</literal></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
<literal>FOR READ ONLY</literal> indicates that the cursor will
|
|
|
|
be used in a read-only mode. <literal>FOR UPDATE</literal>
|
|
|
|
indicates that the cursor will be used to update tables. Since
|
|
|
|
cursor updates are not currently supported in
|
|
|
|
<productname>PostgreSQL</productname>, specifying <literal>FOR
|
|
|
|
UPDATE</literal> will cause an error message and specifying
|
|
|
|
<literal>FOR READ ONLY</literal> has no effect.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable class="parameter">column</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Column(s) to be updated by the cursor. Since cursor updates are
|
|
|
|
not currently supported in
|
|
|
|
<productname>PostgreSQL</productname>, the <literal>FOR
|
|
|
|
UPDATE</literal> clause provokes an error message.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The key words <literal>BINARY</literal>,
|
|
|
|
<literal>INSENSITIVE</literal>, and <literal>SCROLL</literal> may
|
|
|
|
appear in any order.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
2003-05-04 04:23:16 +02:00
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Diagnostics</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>DECLARE CURSOR</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The message returned if the cursor was successfully defined.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>WARNING: Closing pre-existing portal "<replaceable class="parameter">cursorname</replaceable>"</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This message is reported if a cursor with the same name already
|
|
|
|
exists. The previous definition is discarded.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
|
|
|
|
<varlistentry>
|
|
|
|
<term><computeroutput>ERROR: DECLARE CURSOR may only be used in begin/end transaction blocks</computeroutput></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
This error occurs if the cursor is not declared within a
|
|
|
|
transaction block, and <literal>WITH HOLD</literal> is not
|
|
|
|
specified.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
1999-07-22 17:09:15 +02:00
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
If <literal>WITH HOLD</literal> is not specified, the cursor
|
|
|
|
created by this command can only be used within the current
|
|
|
|
transaction. Use
|
2000-12-26 00:15:27 +01:00
|
|
|
<xref linkend="sql-begin" endterm="sql-begin-title">,
|
|
|
|
<xref linkend="sql-commit" endterm="sql-commit-title">
|
1999-07-22 17:09:15 +02:00
|
|
|
and
|
2000-12-26 00:15:27 +01:00
|
|
|
<xref linkend="sql-rollback" endterm="sql-rollback-title">
|
1999-07-22 17:09:15 +02:00
|
|
|
to define a transaction block.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
1999-07-22 17:09:15 +02:00
|
|
|
|
2003-03-10 04:53:52 +01:00
|
|
|
<para>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
If <literal>WITH HOLD</literal> is specified and the transaction
|
2003-05-04 04:23:16 +02:00
|
|
|
that created the cursor successfully commits, the cursor can
|
|
|
|
continue to be accessed by subsequent transactions in the same
|
|
|
|
session. (But if the creating transaction is aborted, the cursor
|
|
|
|
is removed.) A cursor created with <literal>WITH HOLD</literal>
|
|
|
|
is closed when an explicit <command>CLOSE</command> command is
|
|
|
|
issued on it, or the session ends. In the current implementation,
|
|
|
|
the rows represented by a held cursor are copied into a temporary
|
|
|
|
file or memory area so that they remain available for subsequent
|
|
|
|
transactions.
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <literal>SCROLL</> option should be specified when defining a
|
|
|
|
cursor that will be used to fetch backwards. This is required by
|
2003-05-04 04:23:16 +02:00
|
|
|
the SQL standard. However, for compatibility with earlier
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
versions, <productname>PostgreSQL</productname> will allow
|
|
|
|
backward fetches without <literal>SCROLL</>, if the cursor's query
|
|
|
|
plan is simple enough that no extra overhead is needed to support
|
|
|
|
it. However, application developers are advised not to rely on
|
|
|
|
using backward fetches from a cursor that has not been created
|
2003-05-04 04:23:16 +02:00
|
|
|
with <literal>SCROLL</literal>. If <literal>NO SCROLL</> is
|
|
|
|
specified, then backward fetches are disallowed in any case.
|
2003-03-10 04:53:52 +01:00
|
|
|
</para>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
2003-05-04 04:23:16 +02:00
|
|
|
The SQL standard only makes provisions for cursors in embedded
|
|
|
|
<acronym>SQL</acronym>. The <productname>PostgreSQL</productname>
|
|
|
|
server does not implement an <command>OPEN</command> statement for
|
|
|
|
cursors; a cursor is considered to be open when it is declared.
|
|
|
|
However, <application>ECPG</application>, the embedded SQL
|
|
|
|
preprocessor for <productname>PostgreSQL</productname>, supports
|
|
|
|
the standard SQL cursor conventions, including those involving
|
|
|
|
<command>DECLARE</command> and <command>OPEN</command> statements.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
1998-09-01 17:53:09 +02:00
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
|
|
|
|
1999-07-06 19:16:42 +02:00
|
|
|
<para>
|
1998-09-01 17:53:09 +02:00
|
|
|
To declare a cursor:
|
2003-05-04 04:23:16 +02:00
|
|
|
<programlisting>
|
|
|
|
DECLARE liahona CURSOR FOR SELECT * FROM films;
|
|
|
|
</programlisting>
|
|
|
|
See <xref linkend="sql-fetch" endterm="sql-fetch-title"> for more
|
|
|
|
examples of cursor usage.
|
1999-07-06 19:16:42 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
1998-09-01 17:53:09 +02:00
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<para>
|
|
|
|
The SQL standard allows cursors only in embedded
|
|
|
|
<acronym>SQL</acronym> and in modules. <productname>PostgreSQL</>
|
|
|
|
permits cursors to be used interactively.
|
|
|
|
</para>
|
This patch implements holdable cursors, following the proposal
(materialization into a tuple store) discussed on pgsql-hackers earlier.
I've updated the documentation and the regression tests.
Notes on the implementation:
- I needed to change the tuple store API slightly -- it assumes that it
won't be used to hold data across transaction boundaries, so the temp
files that it uses for on-disk storage are automatically reclaimed at
end-of-transaction. I added a flag to tuplestore_begin_heap() to control
this behavior. Is changing the tuple store API in this fashion OK?
- in order to store executor results in a tuple store, I added a new
CommandDest. This works well for the most part, with one exception: the
current DestFunction API doesn't provide enough information to allow the
Executor to store results into an arbitrary tuple store (where the
particular tuple store to use is chosen by the call site of
ExecutorRun). To workaround this, I've temporarily hacked up a solution
that works, but is not ideal: since the receiveTuple DestFunction is
passed the portal name, we can use that to lookup the Portal data
structure for the cursor and then use that to get at the tuple store the
Portal is using. This unnecessarily ties the Portal code with the
tupleReceiver code, but it works...
The proper fix for this is probably to change the DestFunction API --
Tom suggested passing the full QueryDesc to the receiveTuple function.
In that case, callers of ExecutorRun could "subclass" QueryDesc to add
any additional fields that their particular CommandDest needed to get
access to. This approach would work, but I'd like to think about it for
a little bit longer before deciding which route to go. In the mean time,
the code works fine, so I don't think a fix is urgent.
- (semi-related) I added a NO SCROLL keyword to DECLARE CURSOR, and
adjusted the behavior of SCROLL in accordance with the discussion on
-hackers.
- (unrelated) Cleaned up some SGML markup in sql.sgml, copy.sgml
Neil Conway
2003-03-27 17:51:29 +01:00
|
|
|
|
2003-05-04 04:23:16 +02:00
|
|
|
<para>
|
|
|
|
The SQL standard allows cursors to update table data. All
|
|
|
|
<productname>PostgreSQL</> cursors are read only.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Binary cursors are a <productname>PostgreSQL</productname>
|
|
|
|
extension.
|
|
|
|
</para>
|
1998-12-29 03:24:47 +01:00
|
|
|
</refsect1>
|
1999-07-06 19:16:42 +02:00
|
|
|
</refentry>
|
1998-09-01 17:53:09 +02:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
1999-07-06 19:16:42 +02:00
|
|
|
sgml-omittag:nil
|
1998-09-01 17:53:09 +02:00
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
sgml-parent-document:nil
|
|
|
|
sgml-default-dtd-file:"../reference.ced"
|
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
-->
|