2 -- Test large object support
5 CREATE TABLE lotest_stash_values (loid oid, fd integer);
6 -- lo_creat(mode integer) returns oid
7 -- The mode arg to lo_creat is unused, some vestigal holdover from ancient times
8 -- returns the large object id
9 INSERT INTO lotest_stash_values (loid) SELECT lo_creat(42);
10 -- NOTE: large objects require transactions
12 -- lo_open(lobjId oid, mode integer) returns integer
13 -- The mode parameter to lo_open uses two constants:
15 -- INV_WRITE = 0x40000
16 -- The return value is a file descriptor-like value which remains valid for the
18 UPDATE lotest_stash_values SET fd = lo_open(loid, CAST(x'20000' | x'40000' AS integer));
19 -- loread/lowrite names are wonky, different from other functions which are lo_*
20 -- lowrite(fd integer, data bytea) returns integer
21 -- the integer is the number of bytes written
23 Whose woods these are I think I know,
24 His house is in the village though.
25 He will not see me stopping here,
26 To watch his woods fill up with snow.
28 My little horse must think it queer,
29 To stop without a farmhouse near,
30 Between the woods and frozen lake,
31 The darkest evening of the year.
33 He gives his harness bells a shake,
34 To ask if there is some mistake.
35 The only other sound''s the sweep,
36 Of easy wind and downy flake.
38 The woods are lovely, dark and deep,
39 But I have promises to keep,
40 And miles to go before I sleep,
41 And miles to go before I sleep.
44 ') FROM lotest_stash_values;
50 -- lo_close(fd integer) returns integer
51 -- return value is 0 for success, or <0 for error (actually only -1, but...)
52 SELECT lo_close(fd) FROM lotest_stash_values;
61 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
62 -- lo_lseek(fd integer, offset integer, whence integer) returns integer
63 -- offset is in bytes, whence is one of three values:
64 -- SEEK_SET (= 0) meaning relative to beginning
65 -- SEEK_CUR (= 1) meaning relative to current position
66 -- SEEK_END (= 2) meaning relative to end (offset better be negative)
67 -- returns current position in file
68 SELECT lo_lseek(fd, 422, 0) FROM lotest_stash_values;
74 -- loread/lowrite names are wonky, different from other functions which are lo_*
75 -- loread(fd integer, len integer) returns bytea
76 SELECT loread(fd, 35) FROM lotest_stash_values;
78 -------------------------------------
79 The woods are lovely, dark and deep
82 SELECT lo_lseek(fd, -19, 1) FROM lotest_stash_values;
88 SELECT lowrite(fd, 'n') FROM lotest_stash_values;
94 SELECT lo_tell(fd) FROM lotest_stash_values;
100 SELECT lo_lseek(fd, -156, 2) FROM lotest_stash_values;
106 SELECT loread(fd, 35) FROM lotest_stash_values;
108 -------------------------------------
109 The woods are lonely, dark and deep
112 SELECT lo_close(fd) FROM lotest_stash_values;
121 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
122 SELECT lo_truncate(fd, 10) FROM lotest_stash_values;
128 SELECT loread(fd, 15) FROM lotest_stash_values;
134 SELECT lo_truncate(fd, 10000) FROM lotest_stash_values;
140 SELECT loread(fd, 10) FROM lotest_stash_values;
142 ------------------------------------------
143 \000\000\000\000\000\000\000\000\000\000
146 SELECT lo_lseek(fd, 0, 2) FROM lotest_stash_values;
152 SELECT lo_tell(fd) FROM lotest_stash_values;
158 SELECT lo_truncate(fd, 5000) FROM lotest_stash_values;
164 SELECT lo_lseek(fd, 0, 2) FROM lotest_stash_values;
170 SELECT lo_tell(fd) FROM lotest_stash_values;
176 SELECT lo_close(fd) FROM lotest_stash_values;
183 -- lo_unlink(lobjId oid) returns integer
184 -- return value appears to always be 1
185 SELECT lo_unlink(loid) from lotest_stash_values;
191 TRUNCATE lotest_stash_values;
192 INSERT INTO lotest_stash_values (loid) SELECT lo_import('@abs_srcdir@/data/tenk.data');
194 UPDATE lotest_stash_values SET fd=lo_open(loid, CAST(x'20000' | x'40000' AS integer));
195 -- with the default BLKSZ, LOBLKSZ = 2048, so this positions us for a block
197 SELECT lo_lseek(fd, 2030, 0) FROM lotest_stash_values;
203 -- this should get half of the value from page 0 and half from page 1 of the
205 SELECT loread(fd, 36) FROM lotest_stash_values;
207 --------------------------------------------------------------
208 44\011144\0111144\0114144\0119144\01188\01189\011SNAAAA\011F
211 SELECT lo_tell(fd) FROM lotest_stash_values;
217 SELECT lo_lseek(fd, -26, 1) FROM lotest_stash_values;
223 SELECT lowrite(fd, 'abcdefghijklmnop') FROM lotest_stash_values;
229 SELECT lo_lseek(fd, 2030, 0) FROM lotest_stash_values;
235 SELECT loread(fd, 36) FROM lotest_stash_values;
237 --------------------------------------------------
238 44\011144\011114abcdefghijklmnop9\011SNAAAA\011F
241 SELECT lo_close(fd) FROM lotest_stash_values;
248 SELECT lo_export(loid, '@abs_builddir@/results/lotest.txt') FROM lotest_stash_values;
254 \lo_import 'results/lotest.txt'
255 \set newloid :LASTOID
256 -- just make sure \lo_export does not barf
257 \lo_export :newloid 'results/lotest2.txt'
258 -- This is a hack to test that export/import are reversible
259 -- This uses knowledge about the inner workings of large object mechanism
260 -- which should not be used outside it. This makes it a HACK
261 SELECT pageno, data FROM pg_largeobject WHERE loid = (SELECT loid from lotest_stash_values)
263 SELECT pageno, data FROM pg_largeobject WHERE loid = :newloid;
268 SELECT lo_unlink(loid) FROM lotest_stash_values;
275 TRUNCATE lotest_stash_values;