8000 Doc: fix out-of-date example of SPI usage. · postgres/postgres@df73e8b · GitHub
[go: up one dir, main page]

Skip to content

Commit df73e8b

Browse files
committed
Doc: fix out-of-date example of SPI usage.
The "count" argument of SPI_exec() only limits execution when the query is actually returning rows. This was not the case before PG 9.0, so this example was correct when written; but we missed updating it in commit 2ddc600. Extend the example to show the behavior both with and without RETURNING. While here, improve the commentary and markup for the rest of the example. David G. Johnston and Tom Lane, per report from Curt Kolovson. Back-patch to all supported branches. Discussion: https://postgr.es/m/CANhYJV6HWtgz_qjx_APfK0PAgLUzY-2vjLuj7i_o=TZF1LAQew@mail.gmail.com
1 parent 6c7bffc commit df73e8b

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

doc/src/sgml/spi.sgml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4645,15 +4645,16 @@ CREATE FUNCTION execq(text, integer) RETURNS int8
46454645
=> INSERT INTO a VALUES (execq('INSERT INTO a VALUES (0)', 0));
46464646
INSERT 0 1
46474647
=> SELECT execq('SELECT * FROM a', 0);
4648-
INFO: EXECQ: 0 -- inserted by execq
4649-
INFO: EXECQ: 1 -- returned by execq and inserted by upper INSERT
4648+
INFO: EXECQ: 0 <lineannotation>-- inserted by execq</lineannotation>
4649+
INFO: EXECQ: 1 <lineannotation>-- returned by execq and inserted by upper INSERT</lineannotation>
46504650

46514651
execq
46524652
-------
46534653
2
46544654
(1 row)
46554655

4656-
=&gt; SELECT execq('INSERT INTO a SELECT x + 2 FROM a', 1);
4656+
=&gt; SELECT execq('INSERT INTO a SELECT x + 2 FROM a RETURNING *', 1);
4657+
INFO: EXECQ: 2 <lineannotation>-- 0 + 2, then execution was stopped by count</lineannotation>
46574658
execq
46584659
-------
46594660
1
@@ -4662,21 +4663,38 @@ INFO: EXECQ: 1 -- returned by execq and inserted by upper INSERT
46624663
=&gt; SELECT execq('SELECT * FROM a', 10);
46634664
INFO: EXECQ: 0
46644665
INFO: EXECQ: 1
4665-
INFO: EXECQ: 2 -- 0 + 2, only one row inserted - as specified
4666+
INFO: EXECQ: 2
4667+
4668+
execq
4669+
-------
4670+
3 <lineannotation>-- 10 is the max value only, 3 is the real number of rows</lineannotation>
4671+
(1 row)
46664672

4673+
=&gt; SELECT execq('INSERT INTO a SELECT x + 10 FROM a', 1);
46674674
execq
46684675
-------
4669-
3 -- 10 is the max value only, 3 is the real number of rows
4676+
3 <lineannotation>-- all rows processed; count does not stop it, because nothing is returned</lineannotation>
46704677
(1 row)
46714678

4679+
=&gt; SELECT * FROM a;
4680+
x
4681+
----
4682+
0
4683+
1
4684+
2
4685+
10
4686+
11
4687+
12
4688+
(6 rows)
4689+
46724690
=&gt; DELETE FROM a;
4673-
DELETE 3
4691+
DELETE 6
46744692
=&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
46754693
INSERT 0 1
46764694
=&gt; SELECT * FROM a;
46774695
x
46784696
---
4679-
1 -- no rows in a (0) + 1
4697+
1 <lineannotation>-- 0 (no rows in a) + 1</lineannotation>
46804698
(1 row)
46814699

46824700
=&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
@@ -4686,15 +4704,16 @@ INSERT 0 1
46864704
x
46874705
---
46884706
1
4689-
2 -- there was one row in a + 1
4707+
2 <lineannotation>-- 1 (there was one row in a) + 1</lineannotation>
46904708
(2 rows)
46914709

4692-
-- This demonstrates the data changes visibility rule:
4710+
<lineannotation>-- This demonstrates the data changes visibility rule.</lineannotation>
4711+
<lineannotation>-- execq is called twice and sees different numbers of rows each time:</lineannotation>
46934712

46944713
=&gt; INSERT INTO a SELECT execq('SELECT * FROM a', 0) * x FROM a;
4695-
INFO: EXECQ: 1
4714+
INFO: EXECQ: 1 <lineannotation>-- results from first execq</lineannotation>
46964715
INFO: EXECQ: 2
4697-
INFO: EXECQ: 1
4716+
INFO: EXECQ: 1 <lineannotation>-- results from second execq</lineannotation>
46984717
INFO: EXECQ: 2
46994718
INFO: EXECQ: 2
47004719
INSERT 0 2
@@ -4703,10 +4722,9 @@ INSERT 0 2
47034722
---
47044723
1
47054724
2
4706-
2 -- 2 rows * 1 (x in first row)
4707-
6 -- 3 rows (2 + 1 just inserted) * 2 (x in second row)
4708-
(4 rows) ^^^^^^
4709-
rows visible to execq() in different invocations
4725+
2 <lineannotation>-- 2 rows * 1 (x in first row)</lineannotation>
4726+
6 <lineannotation>-- 3 rows (2 + 1 just inserted) * 2 (x in second row)</lineannotation>
4727+
(4 rows)
47104728
</programlisting>
47114729
</para>
47124730
</sect1>

0 commit comments

Comments
 (0)
0