]> granicus.if.org Git - postgresql/blob - src/backend/catalog/pg_largeobject.c
here it is as requested by Bruce.
[postgresql] / src / backend / catalog / pg_largeobject.c
1 /*-------------------------------------------------------------------------
2  *
3  * pg_largeobject.c
4  *        routines to support manipulation of the pg_largeobject relation
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/catalog/pg_largeobject.c,v 1.3 2000/10/21 15:55:21 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
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/fmgroids.h"
24
25 bytea *_byteain(const char *data, int32 size);
26
27 bytea *_byteain(const char *data, int32 size) {
28         bytea   *result;
29
30         result = (bytea *)palloc(size + VARHDRSZ);
31         result->vl_len = size + VARHDRSZ;
32         if (size > 0)
33                 memcpy(result->vl_dat, data, size);
34         
35         return result;
36 }
37
38 Oid LargeobjectCreate(Oid loid) {
39         Oid             retval;
40         Relation                pg_largeobject;
41         HeapTuple       ntup = (HeapTuple) palloc(sizeof(HeapTupleData));
42         Relation                idescs[Num_pg_largeobject_indices];
43         Datum           values[Natts_pg_largeobject];
44         char            nulls[Natts_pg_largeobject];
45         int             i;
46
47         for (i=0; i<Natts_pg_largeobject; i++) {
48                 nulls[i] = ' ';
49                 values[i] = (Datum)NULL;
50         }
51
52         i = 0;
53         values[i++] = ObjectIdGetDatum(loid);
54         values[i++] = Int32GetDatum(0);
55         values[i++] = (Datum) _byteain(NULL, 0);
56         
57         pg_largeobject = heap_openr(LargeobjectRelationName, RowExclusiveLock);
58         ntup = heap_formtuple(pg_largeobject->rd_att, values, nulls);
59         retval = heap_insert(pg_largeobject, ntup);
60
61         if (!IsIgnoringSystemIndexes()) {
62                 CatalogOpenIndices(Num_pg_largeobject_indices, Name_pg_largeobject_indices, idescs);
63                 CatalogIndexInsert(idescs, Num_pg_largeobject_indices, pg_largeobject, ntup);
64                 CatalogCloseIndices(Num_pg_largeobject_indices, idescs);
65         }
66         
67         heap_close(pg_largeobject, RowExclusiveLock);
68         heap_freetuple(ntup);
69         
70         CommandCounterIncrement();
71
72         return retval;
73 }
74
75 void LargeobjectDrop(Oid loid) {
76         Relation        pg_largeobject;
77         Relation        pg_lo_id;
78         ScanKeyData     skey;
79         IndexScanDesc   sd = (IndexScanDesc) NULL;
80         RetrieveIndexResult     indexRes;
81         int     found = 0;
82
83         ScanKeyEntryInitialize(&skey,
84                                             (bits16) 0x0,
85                                             (AttrNumber) 1,
86                                             (RegProcedure) F_OIDEQ,
87                                             ObjectIdGetDatum(loid));
88
89         pg_largeobject = heap_openr(LargeobjectRelationName, RowShareLock);
90         pg_lo_id = index_openr(LargeobjectLOIdIndex);
91
92         sd = index_beginscan(pg_lo_id, false, 1, &skey);
93
94         while((indexRes = index_getnext(sd, ForwardScanDirection))) {
95                 found++;
96                 heap_delete(pg_largeobject, &indexRes->heap_iptr, NULL);
97                 pfree(indexRes);
98         }
99
100         index_endscan(sd);
101
102         index_close(pg_lo_id);
103         heap_close(pg_largeobject, RowShareLock);
104         if (found == 0)
105                 elog(ERROR, "LargeobjectDrop: large object %d not found", loid);
106 }
107
108 int LargeobjectFind(Oid loid) {
109         int     retval = 0;
110         Relation        pg_lo_id;
111         ScanKeyData     skey;
112         IndexScanDesc   sd = (IndexScanDesc) NULL;
113         RetrieveIndexResult     indexRes;
114
115         ScanKeyEntryInitialize(&skey,
116                                             (bits16) 0x0,
117                                             (AttrNumber) 1,
118                                             (RegProcedure) F_OIDEQ,
119                                             ObjectIdGetDatum(loid));
120
121         pg_lo_id = index_openr(LargeobjectLOIdIndex);
122
123         sd = index_beginscan(pg_lo_id, false, 1, &skey);
124
125         if ((indexRes = index_getnext(sd, ForwardScanDirection))) {
126                 retval = 1;
127                 pfree(indexRes);
128         }
129
130         index_endscan(sd);
131
132         index_close(pg_lo_id);
133         return retval;
134 }
135