1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_largeobject relation
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/catalog/pg_largeobject.c,v 1.5 2000/10/24 01:38:23 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "catalog/catname.h"
20 #include "catalog/indexing.h"
21 #include "catalog/pg_largeobject.h"
22 #include "miscadmin.h"
23 #include "utils/builtins.h"
24 #include "utils/fmgroids.h"
28 * Create a large object having the given LO identifier.
30 * We do this by inserting an empty first page, so that the object will
31 * appear to exist with size 0. Note that the unique index will reject
32 * an attempt to create a duplicate page.
34 * Return value is OID assigned to the page tuple (any use in it?)
37 LargeObjectCreate(Oid loid)
40 Relation pg_largeobject;
42 Relation idescs[Num_pg_largeobject_indices];
43 Datum values[Natts_pg_largeobject];
44 char nulls[Natts_pg_largeobject];
47 pg_largeobject = heap_openr(LargeObjectRelationName, RowExclusiveLock);
52 for (i = 0; i < Natts_pg_largeobject; i++)
54 values[i] = (Datum)NULL;
59 values[i++] = ObjectIdGetDatum(loid);
60 values[i++] = Int32GetDatum(0);
61 values[i++] = DirectFunctionCall1(byteain,
64 ntup = heap_formtuple(pg_largeobject->rd_att, values, nulls);
69 retval = heap_insert(pg_largeobject, ntup);
74 if (!IsIgnoringSystemIndexes())
76 CatalogOpenIndices(Num_pg_largeobject_indices, Name_pg_largeobject_indices, idescs);
77 CatalogIndexInsert(idescs, Num_pg_largeobject_indices, pg_largeobject, ntup);
78 CatalogCloseIndices(Num_pg_largeobject_indices, idescs);
81 heap_close(pg_largeobject, RowExclusiveLock);
89 LargeObjectDrop(Oid loid)
92 Relation pg_largeobject;
96 RetrieveIndexResult indexRes;
100 ScanKeyEntryInitialize(&skey[0],
103 (RegProcedure) F_OIDEQ,
104 ObjectIdGetDatum(loid));
106 pg_largeobject = heap_openr(LargeObjectRelationName, RowShareLock);
107 pg_lo_idx = index_openr(LargeObjectLOidPNIndex);
109 sd = index_beginscan(pg_lo_idx, false, 1, skey);
111 tuple.t_datamcxt = CurrentMemoryContext;
114 while ((indexRes = index_getnext(sd, ForwardScanDirection)))
116 tuple.t_self = indexRes->heap_iptr;
117 heap_fetch(pg_largeobject, SnapshotNow, &tuple, &buffer);
119 if (tuple.t_data != NULL)
121 heap_delete(pg_largeobject, &tuple.t_self, NULL);
122 ReleaseBuffer(buffer);
129 index_close(pg_lo_idx);
130 heap_close(pg_largeobject, RowShareLock);
133 elog(ERROR, "LargeObjectDrop: large object %u not found", loid);
137 LargeObjectExists(Oid loid)
140 Relation pg_largeobject;
144 RetrieveIndexResult indexRes;
149 * See if we can find any tuples belonging to the specified LO
151 ScanKeyEntryInitialize(&skey[0],
154 (RegProcedure) F_OIDEQ,
155 ObjectIdGetDatum(loid));
157 pg_largeobject = heap_openr(LargeObjectRelationName, RowShareLock);
158 pg_lo_idx = index_openr(LargeObjectLOidPNIndex);
160 sd = index_beginscan(pg_lo_idx, false, 1, skey);
162 tuple.t_datamcxt = CurrentMemoryContext;
165 while ((indexRes = index_getnext(sd, ForwardScanDirection)))
167 tuple.t_self = indexRes->heap_iptr;
168 heap_fetch(pg_largeobject, SnapshotNow, &tuple, &buffer);
170 if (tuple.t_data != NULL)
173 ReleaseBuffer(buffer);
180 index_close(pg_lo_idx);
181 heap_close(pg_largeobject, RowShareLock);