1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>33.5. Example Program</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="lo-funcs.html" title="33.4. Server-Side Functions" /><link rel="next" href="ecpg.html" title="Chapter 34. ECPG — Embedded SQL in C" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">33.5. Example Program</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="lo-funcs.html" title="33.4. Server-Side Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="largeobjects.html" title="Chapter 33. Large Objects">Up</a></td><th width="60%" align="center">Chapter 33. Large Objects</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg.html" title="Chapter 34. ECPG — Embedded SQL in C">Next</a></td></tr></table><hr /></div><div class="sect1" id="LO-EXAMPLESECT"><div class="titlepage"><div><div><h2 class="title" style="clear: both">33.5. Example Program <a href="#LO-EXAMPLESECT" class="id_link">#</a></h2></div></div></div><p>
3 <a class="xref" href="lo-examplesect.html#LO-EXAMPLE" title="Example 33.1. Large Objects with libpq Example Program">Example 33.1</a> is a sample program which shows how the large object
5 in <span class="application">libpq</span> can be used. Parts of the program are
6 commented out but are left in the source for the reader's
7 benefit. This program can also be found in
8 <code class="filename">src/test/examples/testlo.c</code> in the source distribution.
9 </p><div class="example" id="LO-EXAMPLE"><p class="title"><strong>Example 33.1. Large Objects with <span class="application">libpq</span> Example Program</strong></p><div class="example-contents"><pre class="programlisting">
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 *-----------------------------------------------------------------
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.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");
281 </pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="lo-funcs.html" title="33.4. Server-Side Functions">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="largeobjects.html" title="Chapter 33. Large Objects">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg.html" title="Chapter 34. ECPG — Embedded SQL in C">Next</a></td></tr><tr><td width="40%" align="left" valign="top">33.4. Server-Side Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> Chapter 34. <span class="application">ECPG</span> — Embedded <acronym class="acronym">SQL</acronym> in C</td></tr></table></div></body></html>