]> granicus.if.org Git - postgresql/blob - src/backend/catalog/pg_largeobject.c
Update copyright for 2006. Update scripts.
[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-2006, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/catalog/pg_largeobject.c,v 1.25 2006/03/05 15:58:23 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/genam.h"
18 #include "access/heapam.h"
19 #include "catalog/indexing.h"
20 #include "catalog/pg_largeobject.h"
21 #include "miscadmin.h"
22 #include "utils/builtins.h"
23 #include "utils/fmgroids.h"
24
25
26 /*
27  * Create a large object having the given LO identifier.
28  *
29  * We do this by inserting an empty first page, so that the object will
30  * appear to exist with size 0.  Note that the unique index will reject
31  * an attempt to create a duplicate page.
32  */
33 void
34 LargeObjectCreate(Oid loid)
35 {
36         Relation        pg_largeobject;
37         HeapTuple       ntup;
38         Datum           values[Natts_pg_largeobject];
39         char            nulls[Natts_pg_largeobject];
40         int                     i;
41
42         pg_largeobject = heap_open(LargeObjectRelationId, RowExclusiveLock);
43
44         /*
45          * Form new tuple
46          */
47         for (i = 0; i < Natts_pg_largeobject; i++)
48         {
49                 values[i] = (Datum) NULL;
50                 nulls[i] = ' ';
51         }
52
53         i = 0;
54         values[i++] = ObjectIdGetDatum(loid);
55         values[i++] = Int32GetDatum(0);
56         values[i++] = DirectFunctionCall1(byteain,
57                                                                           CStringGetDatum(""));
58
59         ntup = heap_formtuple(pg_largeobject->rd_att, values, nulls);
60
61         /*
62          * Insert it
63          */
64         simple_heap_insert(pg_largeobject, ntup);
65
66         /* Update indexes */
67         CatalogUpdateIndexes(pg_largeobject, ntup);
68
69         heap_close(pg_largeobject, RowExclusiveLock);
70
71         heap_freetuple(ntup);
72 }
73
74 void
75 LargeObjectDrop(Oid loid)
76 {
77         bool            found = false;
78         Relation        pg_largeobject;
79         ScanKeyData skey[1];
80         SysScanDesc sd;
81         HeapTuple       tuple;
82
83         ScanKeyInit(&skey[0],
84                                 Anum_pg_largeobject_loid,
85                                 BTEqualStrategyNumber, F_OIDEQ,
86                                 ObjectIdGetDatum(loid));
87
88         pg_largeobject = heap_open(LargeObjectRelationId, RowExclusiveLock);
89
90         sd = systable_beginscan(pg_largeobject, LargeObjectLOidPNIndexId, true,
91                                                         SnapshotNow, 1, skey);
92
93         while ((tuple = systable_getnext(sd)) != NULL)
94         {
95                 simple_heap_delete(pg_largeobject, &tuple->t_self);
96                 found = true;
97         }
98
99         systable_endscan(sd);
100
101         heap_close(pg_largeobject, RowExclusiveLock);
102
103         if (!found)
104                 ereport(ERROR,
105                                 (errcode(ERRCODE_UNDEFINED_OBJECT),
106                                  errmsg("large object %u does not exist", loid)));
107 }
108
109 bool
110 LargeObjectExists(Oid loid)
111 {
112         bool            retval = false;
113         Relation        pg_largeobject;
114         ScanKeyData skey[1];
115         SysScanDesc sd;
116
117         /*
118          * See if we can find any tuples belonging to the specified LO
119          */
120         ScanKeyInit(&skey[0],
121                                 Anum_pg_largeobject_loid,
122                                 BTEqualStrategyNumber, F_OIDEQ,
123                                 ObjectIdGetDatum(loid));
124
125         pg_largeobject = heap_open(LargeObjectRelationId, AccessShareLock);
126
127         sd = systable_beginscan(pg_largeobject, LargeObjectLOidPNIndexId, true,
128                                                         SnapshotNow, 1, skey);
129
130         if (systable_getnext(sd) != NULL)
131                 retval = true;
132
133         systable_endscan(sd);
134
135         heap_close(pg_largeobject, AccessShareLock);
136
137         return retval;
138 }