1-- Regression tests for prepareable statements. We query the content
2-- of the pg_prepared_statements view as prepared statements are
3-- created and removed.
4SELECT name, statement, parameter_types FROM pg_prepared_statements;
5 name | statement | parameter_types
6------+-----------+-----------------
7(0 rows)
8
9PREPARE q1 AS SELECT 1 AS a;
10EXECUTE q1;
11 a
12---
13 1
14(1 row)
15
16SELECT name, statement, parameter_types FROM pg_prepared_statements;
17 name |          statement           | parameter_types
18------+------------------------------+-----------------
19 q1   | PREPARE q1 AS SELECT 1 AS a; | {}
20(1 row)
21
22-- should fail
23PREPARE q1 AS SELECT 2;
24ERROR:  prepared statement "q1" already exists
25-- should succeed
26DEALLOCATE q1;
27PREPARE q1 AS SELECT 2;
28EXECUTE q1;
29 ?column?
30----------
31        2
32(1 row)
33
34PREPARE q2 AS SELECT 2 AS b;
35SELECT name, statement, parameter_types FROM pg_prepared_statements;
36 name |          statement           | parameter_types
37------+------------------------------+-----------------
38 q1   | PREPARE q1 AS SELECT 2;      | {}
39 q2   | PREPARE q2 AS SELECT 2 AS b; | {}
40(2 rows)
41
42-- sql92 syntax
43DEALLOCATE PREPARE q1;
44SELECT name, statement, parameter_types FROM pg_prepared_statements;
45 name |          statement           | parameter_types
46------+------------------------------+-----------------
47 q2   | PREPARE q2 AS SELECT 2 AS b; | {}
48(1 row)
49
50DEALLOCATE PREPARE q2;
51-- the view should return the empty set again
52SELECT name, statement, parameter_types FROM pg_prepared_statements;
53 name | statement | parameter_types
54------+-----------+-----------------
55(0 rows)
56
57-- parameterized queries
58PREPARE q2(text) AS
59	SELECT datname, datistemplate, datallowconn
60	FROM pg_database WHERE datname = $1;
61EXECUTE q2('postgres');
62 datname  | datistemplate | datallowconn
63----------+---------------+--------------
64 postgres | f             | t
65(1 row)
66
67PREPARE q3(text, int, float, boolean, oid, smallint) AS
68	SELECT * FROM tenk1 WHERE string4 = $1 AND (four = $2 OR
69	ten = $3::bigint OR true = $4 OR oid = $5 OR odd = $6::int)
70	ORDER BY unique1;
71EXECUTE q3('AAAAxx', 5::smallint, 10.5::float, false, 500::oid, 4::bigint);
72 unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
73---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
74       2 |    2716 |   0 |    2 |   2 |      2 |       2 |        2 |           2 |         2 |        2 |   4 |    5 | CAAAAA   | MAEAAA   | AAAAxx
75     102 |     612 |   0 |    2 |   2 |      2 |       2 |      102 |         102 |       102 |      102 |   4 |    5 | YDAAAA   | OXAAAA   | AAAAxx
76     802 |    2908 |   0 |    2 |   2 |      2 |       2 |      802 |         802 |       802 |      802 |   4 |    5 | WEAAAA   | WHEAAA   | AAAAxx
77     902 |    1104 |   0 |    2 |   2 |      2 |       2 |      902 |         902 |       902 |      902 |   4 |    5 | SIAAAA   | MQBAAA   | AAAAxx
78    1002 |    2580 |   0 |    2 |   2 |      2 |       2 |        2 |        1002 |      1002 |     1002 |   4 |    5 | OMAAAA   | GVDAAA   | AAAAxx
79    1602 |    8148 |   0 |    2 |   2 |      2 |       2 |      602 |        1602 |      1602 |     1602 |   4 |    5 | QJAAAA   | KBMAAA   | AAAAxx
80    1702 |    7940 |   0 |    2 |   2 |      2 |       2 |      702 |        1702 |      1702 |     1702 |   4 |    5 | MNAAAA   | KTLAAA   | AAAAxx
81    2102 |    6184 |   0 |    2 |   2 |      2 |       2 |      102 |         102 |      2102 |     2102 |   4 |    5 | WCAAAA   | WDJAAA   | AAAAxx
82    2202 |    8028 |   0 |    2 |   2 |      2 |       2 |      202 |         202 |      2202 |     2202 |   4 |    5 | SGAAAA   | UWLAAA   | AAAAxx
83    2302 |    7112 |   0 |    2 |   2 |      2 |       2 |      302 |         302 |      2302 |     2302 |   4 |    5 | OKAAAA   | ONKAAA   | AAAAxx
84    2902 |    6816 |   0 |    2 |   2 |      2 |       2 |      902 |         902 |      2902 |     2902 |   4 |    5 | QHAAAA   | ECKAAA   | AAAAxx
85    3202 |    7128 |   0 |    2 |   2 |      2 |       2 |      202 |        1202 |      3202 |     3202 |   4 |    5 | ETAAAA   | EOKAAA   | AAAAxx
86    3902 |    9224 |   0 |    2 |   2 |      2 |       2 |      902 |        1902 |      3902 |     3902 |   4 |    5 | CUAAAA   | UQNAAA   | AAAAxx
87    4102 |    7676 |   0 |    2 |   2 |      2 |       2 |      102 |         102 |      4102 |     4102 |   4 |    5 | UBAAAA   | GJLAAA   | AAAAxx
88    4202 |    6628 |   0 |    2 |   2 |      2 |       2 |      202 |         202 |      4202 |     4202 |   4 |    5 | QFAAAA   | YUJAAA   | AAAAxx
89    4502 |     412 |   0 |    2 |   2 |      2 |       2 |      502 |         502 |      4502 |     4502 |   4 |    5 | ERAAAA   | WPAAAA   | AAAAxx
90    4702 |    2520 |   0 |    2 |   2 |      2 |       2 |      702 |         702 |      4702 |     4702 |   4 |    5 | WYAAAA   | YSDAAA   | AAAAxx
91    4902 |    1600 |   0 |    2 |   2 |      2 |       2 |      902 |         902 |      4902 |     4902 |   4 |    5 | OGAAAA   | OJCAAA   | AAAAxx
92    5602 |    8796 |   0 |    2 |   2 |      2 |       2 |      602 |        1602 |       602 |     5602 |   4 |    5 | MHAAAA   | IANAAA   | AAAAxx
93    6002 |    8932 |   0 |    2 |   2 |      2 |       2 |        2 |           2 |      1002 |     6002 |   4 |    5 | WWAAAA   | OFNAAA   | AAAAxx
94    6402 |    3808 |   0 |    2 |   2 |      2 |       2 |      402 |         402 |      1402 |     6402 |   4 |    5 | GMAAAA   | MQFAAA   | AAAAxx
95    7602 |    1040 |   0 |    2 |   2 |      2 |       2 |      602 |        1602 |      2602 |     7602 |   4 |    5 | KGAAAA   | AOBAAA   | AAAAxx
96    7802 |    7508 |   0 |    2 |   2 |      2 |       2 |      802 |        1802 |      2802 |     7802 |   4 |    5 | COAAAA   | UCLAAA   | AAAAxx
97    8002 |    9980 |   0 |    2 |   2 |      2 |       2 |        2 |           2 |      3002 |     8002 |   4 |    5 | UVAAAA   | WTOAAA   | AAAAxx
98    8302 |    7800 |   0 |    2 |   2 |      2 |       2 |      302 |         302 |      3302 |     8302 |   4 |    5 | IHAAAA   | AOLAAA   | AAAAxx
99    8402 |    5708 |   0 |    2 |   2 |      2 |       2 |      402 |         402 |      3402 |     8402 |   4 |    5 | ELAAAA   | OLIAAA   | AAAAxx
100    8602 |    5440 |   0 |    2 |   2 |      2 |       2 |      602 |         602 |      3602 |     8602 |   4 |    5 | WSAAAA   | GBIAAA   | AAAAxx
101    9502 |    1812 |   0 |    2 |   2 |      2 |       2 |      502 |        1502 |      4502 |     9502 |   4 |    5 | MBAAAA   | SRCAAA   | AAAAxx
102    9602 |    9972 |   0 |    2 |   2 |      2 |       2 |      602 |        1602 |      4602 |     9602 |   4 |    5 | IFAAAA   | OTOAAA   | AAAAxx
103(29 rows)
104
105-- too few params
106EXECUTE q3('bool');
107ERROR:  wrong number of parameters for prepared statement "q3"
108DETAIL:  Expected 6 parameters but got 1.
109-- too many params
110EXECUTE q3('bytea', 5::smallint, 10.5::float, false, 500::oid, 4::bigint, true);
111ERROR:  wrong number of parameters for prepared statement "q3"
112DETAIL:  Expected 6 parameters but got 7.
113-- wrong param types
114EXECUTE q3(5::smallint, 10.5::float, false, 500::oid, 4::bigint, 'bytea');
115ERROR:  parameter $3 of type boolean cannot be coerced to the expected type double precision
116HINT:  You will need to rewrite or cast the expression.
117-- invalid type
118PREPARE q4(nonexistenttype) AS SELECT $1;
119ERROR:  type "nonexistenttype" does not exist
120LINE 1: PREPARE q4(nonexistenttype) AS SELECT $1;
121                   ^
122-- create table as execute
123PREPARE q5(int, text) AS
124	SELECT * FROM tenk1 WHERE unique1 = $1 OR stringu1 = $2
125	ORDER BY unique1;
126CREATE TEMPORARY TABLE q5_prep_results AS EXECUTE q5(200, 'DTAAAA');
127SELECT * FROM q5_prep_results;
128 unique1 | unique2 | two | four | ten | twenty | hundred | thousand | twothousand | fivethous | tenthous | odd | even | stringu1 | stringu2 | string4
129---------+---------+-----+------+-----+--------+---------+----------+-------------+-----------+----------+-----+------+----------+----------+---------
130     200 |    9441 |   0 |    0 |   0 |      0 |       0 |      200 |         200 |       200 |      200 |   0 |    1 | SHAAAA   | DZNAAA   | HHHHxx
131     497 |    9092 |   1 |    1 |   7 |     17 |      97 |      497 |         497 |       497 |      497 | 194 |  195 | DTAAAA   | SLNAAA   | AAAAxx
132    1173 |    6699 |   1 |    1 |   3 |     13 |      73 |      173 |        1173 |      1173 |     1173 | 146 |  147 | DTAAAA   | RXJAAA   | VVVVxx
133    1849 |    8143 |   1 |    1 |   9 |      9 |      49 |      849 |        1849 |      1849 |     1849 |  98 |   99 | DTAAAA   | FBMAAA   | VVVVxx
134    2525 |      64 |   1 |    1 |   5 |      5 |      25 |      525 |         525 |      2525 |     2525 |  50 |   51 | DTAAAA   | MCAAAA   | AAAAxx
135    3201 |    7309 |   1 |    1 |   1 |      1 |       1 |      201 |        1201 |      3201 |     3201 |   2 |    3 | DTAAAA   | DVKAAA   | HHHHxx
136    3877 |    4060 |   1 |    1 |   7 |     17 |      77 |      877 |        1877 |      3877 |     3877 | 154 |  155 | DTAAAA   | EAGAAA   | AAAAxx
137    4553 |    4113 |   1 |    1 |   3 |     13 |      53 |      553 |         553 |      4553 |     4553 | 106 |  107 | DTAAAA   | FCGAAA   | HHHHxx
138    5229 |    6407 |   1 |    1 |   9 |      9 |      29 |      229 |        1229 |       229 |     5229 |  58 |   59 | DTAAAA   | LMJAAA   | VVVVxx
139    5905 |    9537 |   1 |    1 |   5 |      5 |       5 |      905 |        1905 |       905 |     5905 |  10 |   11 | DTAAAA   | VCOAAA   | HHHHxx
140    6581 |    4686 |   1 |    1 |   1 |      1 |      81 |      581 |         581 |      1581 |     6581 | 162 |  163 | DTAAAA   | GYGAAA   | OOOOxx
141    7257 |    1895 |   1 |    1 |   7 |     17 |      57 |      257 |        1257 |      2257 |     7257 | 114 |  115 | DTAAAA   | XUCAAA   | VVVVxx
142    7933 |    4514 |   1 |    1 |   3 |     13 |      33 |      933 |        1933 |      2933 |     7933 |  66 |   67 | DTAAAA   | QRGAAA   | OOOOxx
143    8609 |    5918 |   1 |    1 |   9 |      9 |       9 |      609 |         609 |      3609 |     8609 |  18 |   19 | DTAAAA   | QTIAAA   | OOOOxx
144    9285 |    8469 |   1 |    1 |   5 |      5 |      85 |      285 |        1285 |      4285 |     9285 | 170 |  171 | DTAAAA   | TNMAAA   | HHHHxx
145    9961 |    2058 |   1 |    1 |   1 |      1 |      61 |      961 |        1961 |      4961 |     9961 | 122 |  123 | DTAAAA   | EBDAAA   | OOOOxx
146(16 rows)
147
148-- unknown or unspecified parameter types: should succeed
149PREPARE q6 AS
150    SELECT * FROM tenk1 WHERE unique1 = $1 AND stringu1 = $2;
151PREPARE q7(unknown) AS
152    SELECT * FROM road WHERE thepath = $1;
153SELECT name, statement, parameter_types FROM pg_prepared_statements
154    ORDER BY name;
155 name |                              statement                              |                    parameter_types
156------+---------------------------------------------------------------------+--------------------------------------------------------
157 q2   | PREPARE q2(text) AS                                                +| {text}
158      |         SELECT datname, datistemplate, datallowconn                +|
159      |         FROM pg_database WHERE datname = $1;                        |
160 q3   | PREPARE q3(text, int, float, boolean, oid, smallint) AS            +| {text,integer,"double precision",boolean,oid,smallint}
161      |         SELECT * FROM tenk1 WHERE string4 = $1 AND (four = $2 OR   +|
162      |         ten = $3::bigint OR true = $4 OR oid = $5 OR odd = $6::int)+|
163      |         ORDER BY unique1;                                           |
164 q5   | PREPARE q5(int, text) AS                                           +| {integer,text}
165      |         SELECT * FROM tenk1 WHERE unique1 = $1 OR stringu1 = $2    +|
166      |         ORDER BY unique1;                                           |
167 q6   | PREPARE q6 AS                                                      +| {integer,name}
168      |     SELECT * FROM tenk1 WHERE unique1 = $1 AND stringu1 = $2;       |
169 q7   | PREPARE q7(unknown) AS                                             +| {path}
170      |     SELECT * FROM road WHERE thepath = $1;                          |
171(5 rows)
172
173-- test DEALLOCATE ALL;
174DEALLOCATE ALL;
175SELECT name, statement, parameter_types FROM pg_prepared_statements
176    ORDER BY name;
177 name | statement | parameter_types
178------+-----------+-----------------
179(0 rows)
180
181