1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_largeobject relation
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/catalog/pg_largeobject.c
13 *-------------------------------------------------------------------------
17 #include "access/genam.h"
18 #include "access/htup_details.h"
19 #include "access/sysattr.h"
20 #include "access/table.h"
21 #include "catalog/catalog.h"
22 #include "catalog/dependency.h"
23 #include "catalog/indexing.h"
24 #include "catalog/pg_largeobject.h"
25 #include "catalog/pg_largeobject_metadata.h"
26 #include "miscadmin.h"
27 #include "utils/acl.h"
28 #include "utils/fmgroids.h"
29 #include "utils/rel.h"
33 * Create a large object having the given LO identifier.
35 * We create a new large object by inserting an entry into
36 * pg_largeobject_metadata without any data pages, so that the object
37 * will appear to exist with size 0.
40 LargeObjectCreate(Oid loid)
45 Datum values[Natts_pg_largeobject_metadata];
46 bool nulls[Natts_pg_largeobject_metadata];
48 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
52 * Insert metadata of the largeobject
54 memset(values, 0, sizeof(values));
55 memset(nulls, false, sizeof(nulls));
60 loid_new = GetNewOidWithIndex(pg_lo_meta,
61 LargeObjectMetadataOidIndexId,
62 Anum_pg_largeobject_metadata_oid);
64 values[Anum_pg_largeobject_metadata_oid - 1] = ObjectIdGetDatum(loid_new);
65 values[Anum_pg_largeobject_metadata_lomowner - 1]
66 = ObjectIdGetDatum(GetUserId());
67 nulls[Anum_pg_largeobject_metadata_lomacl - 1] = true;
69 ntup = heap_form_tuple(RelationGetDescr(pg_lo_meta),
72 CatalogTupleInsert(pg_lo_meta, ntup);
76 table_close(pg_lo_meta, RowExclusiveLock);
82 * Drop a large object having the given LO identifier. Both the data pages
83 * and metadata must be dropped.
86 LargeObjectDrop(Oid loid)
89 Relation pg_largeobject;
94 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
97 pg_largeobject = table_open(LargeObjectRelationId,
101 * Delete an entry from pg_largeobject_metadata
103 ScanKeyInit(&skey[0],
104 Anum_pg_largeobject_metadata_oid,
105 BTEqualStrategyNumber, F_OIDEQ,
106 ObjectIdGetDatum(loid));
108 scan = systable_beginscan(pg_lo_meta,
109 LargeObjectMetadataOidIndexId, true,
112 tuple = systable_getnext(scan);
113 if (!HeapTupleIsValid(tuple))
115 (errcode(ERRCODE_UNDEFINED_OBJECT),
116 errmsg("large object %u does not exist", loid)));
118 CatalogTupleDelete(pg_lo_meta, &tuple->t_self);
120 systable_endscan(scan);
123 * Delete all the associated entries from pg_largeobject
125 ScanKeyInit(&skey[0],
126 Anum_pg_largeobject_loid,
127 BTEqualStrategyNumber, F_OIDEQ,
128 ObjectIdGetDatum(loid));
130 scan = systable_beginscan(pg_largeobject,
131 LargeObjectLOidPNIndexId, true,
133 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
135 CatalogTupleDelete(pg_largeobject, &tuple->t_self);
138 systable_endscan(scan);
140 table_close(pg_largeobject, RowExclusiveLock);
142 table_close(pg_lo_meta, RowExclusiveLock);
148 * We don't use the system cache for large object metadata, for fear of
149 * using too much local memory.
151 * This function always scans the system catalog using an up-to-date snapshot,
152 * so it should not be used when a large object is opened in read-only mode
153 * (because large objects opened in read only mode are supposed to be viewed
154 * relative to the caller's snapshot, whereas in read-write mode they are
155 * relative to a current snapshot).
158 LargeObjectExists(Oid loid)
166 ScanKeyInit(&skey[0],
167 Anum_pg_largeobject_metadata_oid,
168 BTEqualStrategyNumber, F_OIDEQ,
169 ObjectIdGetDatum(loid));
171 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
174 sd = systable_beginscan(pg_lo_meta,
175 LargeObjectMetadataOidIndexId, true,
178 tuple = systable_getnext(sd);
179 if (HeapTupleIsValid(tuple))
182 systable_endscan(sd);
184 table_close(pg_lo_meta, AccessShareLock);