]> granicus.if.org Git - postgresql/blob - src/backend/catalog/pg_largeobject.c
Error message editing in backend/catalog.
[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-2002, PostgreSQL Global Development Group
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.15 2003/07/21 01:59:10 tgl 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/builtins.h"
24 #include "utils/fmgroids.h"
25
26
27 /*
28  * Create a large object having the given LO identifier.
29  *
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.
33  */
34 void
35 LargeObjectCreate(Oid loid)
36 {
37         Relation        pg_largeobject;
38         HeapTuple       ntup;
39         Datum           values[Natts_pg_largeobject];
40         char            nulls[Natts_pg_largeobject];
41         int                     i;
42
43         pg_largeobject = heap_openr(LargeObjectRelationName, RowExclusiveLock);
44
45         /*
46          * Form new tuple
47          */
48         for (i = 0; i < Natts_pg_largeobject; i++)
49         {
50                 values[i] = (Datum) NULL;
51                 nulls[i] = ' ';
52         }
53
54         i = 0;
55         values[i++] = ObjectIdGetDatum(loid);
56         values[i++] = Int32GetDatum(0);
57         values[i++] = DirectFunctionCall1(byteain,
58                                                                           CStringGetDatum(""));
59
60         ntup = heap_formtuple(pg_largeobject->rd_att, values, nulls);
61
62         /*
63          * Insert it
64          */
65         simple_heap_insert(pg_largeobject, ntup);
66
67         /* Update indexes */
68         CatalogUpdateIndexes(pg_largeobject, ntup);
69
70         heap_close(pg_largeobject, RowExclusiveLock);
71
72         heap_freetuple(ntup);
73 }
74
75 void
76 LargeObjectDrop(Oid loid)
77 {
78         bool            found = false;
79         Relation        pg_largeobject;
80         Relation        pg_lo_idx;
81         ScanKeyData skey[1];
82         IndexScanDesc sd;
83         HeapTuple       tuple;
84
85         ScanKeyEntryInitialize(&skey[0],
86                                                    (bits16) 0x0,
87                                                    (AttrNumber) 1,
88                                                    (RegProcedure) F_OIDEQ,
89                                                    ObjectIdGetDatum(loid));
90
91         pg_largeobject = heap_openr(LargeObjectRelationName, RowShareLock);
92         pg_lo_idx = index_openr(LargeObjectLOidPNIndex);
93
94         sd = index_beginscan(pg_largeobject, pg_lo_idx, SnapshotNow, 1, skey);
95
96         while ((tuple = index_getnext(sd, ForwardScanDirection)) != NULL)
97         {
98                 simple_heap_delete(pg_largeobject, &tuple->t_self);
99                 found = true;
100         }
101
102         index_endscan(sd);
103
104         index_close(pg_lo_idx);
105         heap_close(pg_largeobject, RowShareLock);
106
107         if (!found)
108                 ereport(ERROR,
109                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
110                                  errmsg("large object %u does not exist", loid)));
111 }
112
113 bool
114 LargeObjectExists(Oid loid)
115 {
116         bool            retval = false;
117         Relation        pg_largeobject;
118         Relation        pg_lo_idx;
119         ScanKeyData skey[1];
120         IndexScanDesc sd;
121         HeapTuple       tuple;
122
123         /*
124          * See if we can find any tuples belonging to the specified LO
125          */
126         ScanKeyEntryInitialize(&skey[0],
127                                                    (bits16) 0x0,
128                                                    (AttrNumber) 1,
129                                                    (RegProcedure) F_OIDEQ,
130                                                    ObjectIdGetDatum(loid));
131
132         pg_largeobject = heap_openr(LargeObjectRelationName, RowShareLock);
133         pg_lo_idx = index_openr(LargeObjectLOidPNIndex);
134
135         sd = index_beginscan(pg_largeobject, pg_lo_idx, SnapshotNow, 1, skey);
136
137         if ((tuple = index_getnext(sd, ForwardScanDirection)) != NULL)
138                 retval = true;
139
140         index_endscan(sd);
141
142         index_close(pg_lo_idx);
143         heap_close(pg_largeobject, RowShareLock);
144
145         return retval;
146 }