]> granicus.if.org Git - postgresql/blob - src/test/regress/output/largeobject.source
Update copyright for the year 2010.
[postgresql] / src / test / regress / output / largeobject.source
1 --
2 -- Test large object support
3 --
4 -- ensure consistent test output regardless of the default bytea format
5 SET bytea_output TO escape;
6 -- Load a file
7 CREATE TABLE lotest_stash_values (loid oid, fd integer);
8 -- lo_creat(mode integer) returns oid
9 -- The mode arg to lo_creat is unused, some vestigal holdover from ancient times
10 -- returns the large object id
11 INSERT INTO lotest_stash_values (loid) SELECT lo_creat(42);
12 -- NOTE: large objects require transactions
13 BEGIN;
14 -- lo_open(lobjId oid, mode integer) returns integer
15 -- The mode parameter to lo_open uses two constants:
16 --   INV_READ  = 0x20000
17 --   INV_WRITE = 0x40000
18 -- The return value is a file descriptor-like value which remains valid for the
19 -- transaction.
20 UPDATE lotest_stash_values SET fd = lo_open(loid, CAST(x'20000' | x'40000' AS integer));
21 -- loread/lowrite names are wonky, different from other functions which are lo_*
22 -- lowrite(fd integer, data bytea) returns integer
23 -- the integer is the number of bytes written
24 SELECT lowrite(fd, '
25 Whose woods these are I think I know,
26 His house is in the village though.
27 He will not see me stopping here,
28 To watch his woods fill up with snow.
29
30 My little horse must think it queer,
31 To stop without a farmhouse near,
32 Between the woods and frozen lake,
33 The darkest evening of the year.
34
35 He gives his harness bells a shake,
36 To ask if there is some mistake.
37 The only other sound''s the sweep,
38 Of easy wind and downy flake.
39
40 The woods are lovely, dark and deep,
41 But I have promises to keep,
42 And miles to go before I sleep,
43 And miles to go before I sleep.
44
45          -- Robert Frost
46 ') FROM lotest_stash_values;
47  lowrite 
48 ---------
49      578
50 (1 row)
51
52 -- lo_close(fd integer) returns integer
53 -- return value is 0 for success, or <0 for error (actually only -1, but...)
54 SELECT lo_close(fd) FROM lotest_stash_values;
55  lo_close 
56 ----------
57         0
58 (1 row)
59
60 END;
61 -- Read out a portion
62 BEGIN;
63 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
64 -- lo_lseek(fd integer, offset integer, whence integer) returns integer
65 -- offset is in bytes, whence is one of three values:
66 --  SEEK_SET (= 0) meaning relative to beginning
67 --  SEEK_CUR (= 1) meaning relative to current position
68 --  SEEK_END (= 2) meaning relative to end (offset better be negative)
69 -- returns current position in file
70 SELECT lo_lseek(fd, 422, 0) FROM lotest_stash_values;
71  lo_lseek 
72 ----------
73       422
74 (1 row)
75
76 -- loread/lowrite names are wonky, different from other functions which are lo_*
77 -- loread(fd integer, len integer) returns bytea
78 SELECT loread(fd, 35) FROM lotest_stash_values;
79                loread                
80 -------------------------------------
81  The woods are lovely, dark and deep
82 (1 row)
83
84 SELECT lo_lseek(fd, -19, 1) FROM lotest_stash_values;
85  lo_lseek 
86 ----------
87       438
88 (1 row)
89
90 SELECT lowrite(fd, 'n') FROM lotest_stash_values;
91  lowrite 
92 ---------
93        1
94 (1 row)
95
96 SELECT lo_tell(fd) FROM lotest_stash_values;
97  lo_tell 
98 ---------
99      439
100 (1 row)
101
102 SELECT lo_lseek(fd, -156, 2) FROM lotest_stash_values;
103  lo_lseek 
104 ----------
105       422
106 (1 row)
107
108 SELECT loread(fd, 35) FROM lotest_stash_values;
109                loread                
110 -------------------------------------
111  The woods are lonely, dark and deep
112 (1 row)
113
114 SELECT lo_close(fd) FROM lotest_stash_values;
115  lo_close 
116 ----------
117         0
118 (1 row)
119
120 END;
121 -- Test resource management
122 BEGIN;
123 SELECT lo_open(loid, x'40000'::int) from lotest_stash_values;
124  lo_open 
125 ---------
126        0
127 (1 row)
128
129 ABORT;
130 -- Test truncation.
131 BEGIN;
132 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
133 SELECT lo_truncate(fd, 10) FROM lotest_stash_values;
134  lo_truncate 
135 -------------
136            0
137 (1 row)
138
139 SELECT loread(fd, 15) FROM lotest_stash_values;
140     loread     
141 ---------------
142  \012Whose woo
143 (1 row)
144
145 SELECT lo_truncate(fd, 10000) FROM lotest_stash_values;
146  lo_truncate 
147 -------------
148            0
149 (1 row)
150
151 SELECT loread(fd, 10) FROM lotest_stash_values;
152                   loread                  
153 ------------------------------------------
154  \000\000\000\000\000\000\000\000\000\000
155 (1 row)
156
157 SELECT lo_lseek(fd, 0, 2) FROM lotest_stash_values;
158  lo_lseek 
159 ----------
160     10000
161 (1 row)
162
163 SELECT lo_tell(fd) FROM lotest_stash_values;
164  lo_tell 
165 ---------
166    10000
167 (1 row)
168
169 SELECT lo_truncate(fd, 5000) FROM lotest_stash_values;
170  lo_truncate 
171 -------------
172            0
173 (1 row)
174
175 SELECT lo_lseek(fd, 0, 2) FROM lotest_stash_values;
176  lo_lseek 
177 ----------
178      5000
179 (1 row)
180
181 SELECT lo_tell(fd) FROM lotest_stash_values;
182  lo_tell 
183 ---------
184     5000
185 (1 row)
186
187 SELECT lo_close(fd) FROM lotest_stash_values;
188  lo_close 
189 ----------
190         0
191 (1 row)
192
193 END;
194 -- lo_unlink(lobjId oid) returns integer
195 -- return value appears to always be 1
196 SELECT lo_unlink(loid) from lotest_stash_values;
197  lo_unlink 
198 -----------
199          1
200 (1 row)
201
202 TRUNCATE lotest_stash_values;
203 INSERT INTO lotest_stash_values (loid) SELECT lo_import('@abs_srcdir@/data/tenk.data');
204 BEGIN;
205 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
206 -- with the default BLKSZ, LOBLKSZ = 2048, so this positions us for a block
207 -- edge case
208 SELECT lo_lseek(fd, 2030, 0) FROM lotest_stash_values;
209  lo_lseek 
210 ----------
211      2030
212 (1 row)
213
214 -- this should get half of the value from page 0 and half from page 1 of the
215 -- large object
216 SELECT loread(fd, 36) FROM lotest_stash_values;
217                              loread                              
218 -----------------------------------------------------------------
219  AAA\011FBAAAA\011VVVVxx\0122513\01132\0111\0111\0113\01113\0111
220 (1 row)
221
222 SELECT lo_tell(fd) FROM lotest_stash_values;
223  lo_tell 
224 ---------
225     2066
226 (1 row)
227
228 SELECT lo_lseek(fd, -26, 1) FROM lotest_stash_values;
229  lo_lseek 
230 ----------
231      2040
232 (1 row)
233
234 SELECT lowrite(fd, 'abcdefghijklmnop') FROM lotest_stash_values;
235  lowrite 
236 ---------
237       16
238 (1 row)
239
240 SELECT lo_lseek(fd, 2030, 0) FROM lotest_stash_values;
241  lo_lseek 
242 ----------
243      2030
244 (1 row)
245
246 SELECT loread(fd, 36) FROM lotest_stash_values;
247                        loread                        
248 -----------------------------------------------------
249  AAA\011FBAAAAabcdefghijklmnop1\0111\0113\01113\0111
250 (1 row)
251
252 SELECT lo_close(fd) FROM lotest_stash_values;
253  lo_close 
254 ----------
255         0
256 (1 row)
257
258 END;
259 SELECT lo_export(loid, '@abs_builddir@/results/lotest.txt') FROM lotest_stash_values;
260  lo_export 
261 -----------
262          1
263 (1 row)
264
265 \lo_import 'results/lotest.txt'
266 \set newloid :LASTOID
267 -- just make sure \lo_export does not barf
268 \lo_export :newloid 'results/lotest2.txt'
269 -- This is a hack to test that export/import are reversible
270 -- This uses knowledge about the inner workings of large object mechanism
271 -- which should not be used outside it.  This makes it a HACK
272 SELECT pageno, data FROM pg_largeobject WHERE loid = (SELECT loid from lotest_stash_values)
273 EXCEPT
274 SELECT pageno, data FROM pg_largeobject WHERE loid = :newloid;
275  pageno | data 
276 --------+------
277 (0 rows)
278
279 SELECT lo_unlink(loid) FROM lotest_stash_values;
280  lo_unlink 
281 -----------
282          1
283 (1 row)
284
285 \lo_unlink :newloid
286 TRUNCATE lotest_stash_values;