2 33.5. Example Program #
4 Example 33.1 is a sample program which shows how the large object
5 interface in libpq can be used. Parts of the program are commented out
6 but are left in the source for the reader's benefit. This program can
7 also be found in src/test/examples/testlo.c in the source distribution.
9 Example 33.1. Large Objects with libpq Example Program
10 /*-----------------------------------------------------------------
13 * test using large objects with libpq
15 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
16 * Portions Copyright (c) 1994, Regents of the University of California
20 * src/test/examples/testlo.c
22 *-----------------------------------------------------------------
27 #include <sys/types.h>
33 #include "libpq/libpq-fs.h"
39 * import file "in_filename" into database as large object "lobjOid"
43 importFile(PGconn *conn, char *filename)
53 * open the file to be read in
55 fd = open(filename, O_RDONLY, 0666);
58 fprintf(stderr, "cannot open unix file\"%s\"\n", filename);
62 * create the large object
64 lobjId = lo_creat(conn, INV_READ | INV_WRITE);
66 fprintf(stderr, "cannot create large object");
68 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
71 * read in from the Unix file and write to the inversion file
73 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
75 tmp = lo_write(conn, lobj_fd, buf, nbytes);
77 fprintf(stderr, "error while reading \"%s\"", filename);
81 lo_close(conn, lobj_fd);
87 pickout(PGconn *conn, Oid lobjId, int start, int len)
94 lobj_fd = lo_open(conn, lobjId, INV_READ);
96 fprintf(stderr, "cannot open large object %u", lobjId);
98 lo_lseek(conn, lobj_fd, start, SEEK_SET);
99 buf = malloc(len + 1);
102 while (len - nread > 0)
104 nbytes = lo_read(conn, lobj_fd, buf, len - nread);
106 fprintf(stderr, ">>> %s", buf);
109 break; /* no more data? */
112 fprintf(stderr, "\n");
113 lo_close(conn, lobj_fd);
117 overwrite(PGconn *conn, Oid lobjId, int start, int len)
125 lobj_fd = lo_open(conn, lobjId, INV_WRITE);
127 fprintf(stderr, "cannot open large object %u", lobjId);
129 lo_lseek(conn, lobj_fd, start, SEEK_SET);
130 buf = malloc(len + 1);
132 for (i = 0; i < len; i++)
137 while (len - nwritten > 0)
139 nbytes = lo_write(conn, lobj_fd, buf + nwritten, len - nwritten);
143 fprintf(stderr, "\nWRITE FAILED!\n");
148 fprintf(stderr, "\n");
149 lo_close(conn, lobj_fd);
155 * export large object "lobjOid" to file "out_filename"
159 exportFile(PGconn *conn, Oid lobjId, char *filename)
168 * open the large object
170 lobj_fd = lo_open(conn, lobjId, INV_READ);
172 fprintf(stderr, "cannot open large object %u", lobjId);
175 * open the file to be written to
177 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
180 fprintf(stderr, "cannot open unix file\"%s\"",
185 * read in from the inversion file and write to the Unix file
187 while ((nbytes = lo_read(conn, lobj_fd, buf, BUFSIZE)) > 0)
189 tmp = write(fd, buf, nbytes);
192 fprintf(stderr, "error while writing \"%s\"",
197 lo_close(conn, lobj_fd);
202 exit_nicely(PGconn *conn)
209 main(int argc, char **argv)
220 fprintf(stderr, "Usage: %s database_name in_filename out_filename\n",
226 in_filename = argv[2];
227 out_filename = argv[3];
230 * set up the connection
232 conn = PQsetdb(NULL, NULL, NULL, NULL, database);
234 /* check to see that the backend connection was successfully made */
235 if (PQstatus(conn) != CONNECTION_OK)
237 fprintf(stderr, "%s", PQerrorMessage(conn));
241 /* Set always-secure search path, so malicious users can't take control. */
243 "SELECT pg_catalog.set_config('search_path', '', false)");
244 if (PQresultStatus(res) != PGRES_TUPLES_OK)
246 fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
252 res = PQexec(conn, "begin");
254 printf("importing file \"%s\" ...\n", in_filename);
255 /* lobjOid = importFile(conn, in_filename); */
256 lobjOid = lo_import(conn, in_filename);
258 fprintf(stderr, "%s\n", PQerrorMessage(conn));
261 printf("\tas large object %u.\n", lobjOid);
263 printf("picking out bytes 1000-2000 of the large object\n");
264 pickout(conn, lobjOid, 1000, 1000);
266 printf("overwriting bytes 1000-2000 of the large object with X's\n");
267 overwrite(conn, lobjOid, 1000, 1000);
269 printf("exporting large object to file \"%s\" ...\n", out_filename);
270 /* exportFile(conn, lobjOid, out_filename); */
271 if (lo_export(conn, lobjOid, out_filename) < 0)
272 fprintf(stderr, "%s\n", PQerrorMessage(conn));
275 res = PQexec(conn, "end");