PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pg_largeobject.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pg_largeobject.c
4 * routines to support manipulation of the pg_largeobject relation
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/catalog/pg_largeobject.c
12 *
13 *-------------------------------------------------------------------------
14 */
15#include "postgres.h"
16
17#include "access/htup_details.h"
18#include "access/table.h"
19#include "catalog/catalog.h"
20#include "catalog/indexing.h"
23#include "miscadmin.h"
24#include "utils/acl.h"
25#include "utils/fmgroids.h"
26#include "utils/rel.h"
27
28
29/*
30 * Create a large object having the given LO identifier.
31 *
32 * We create a new large object by inserting an entry into
33 * pg_largeobject_metadata without any data pages, so that the object
34 * will appear to exist with size 0.
35 */
36Oid
38{
39 Relation pg_lo_meta;
40 HeapTuple ntup;
41 Oid loid_new;
42 Datum values[Natts_pg_largeobject_metadata];
43 bool nulls[Natts_pg_largeobject_metadata];
44 Oid ownerId;
45 Acl *lomacl;
46
47 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
49
50 /*
51 * Insert metadata of the largeobject
52 */
53 memset(values, 0, sizeof(values));
54 memset(nulls, false, sizeof(nulls));
55
56 if (OidIsValid(loid))
57 loid_new = loid;
58 else
59 loid_new = GetNewOidWithIndex(pg_lo_meta,
60 LargeObjectMetadataOidIndexId,
61 Anum_pg_largeobject_metadata_oid);
62 ownerId = GetUserId();
64
65 values[Anum_pg_largeobject_metadata_oid - 1] = ObjectIdGetDatum(loid_new);
66 values[Anum_pg_largeobject_metadata_lomowner - 1]
67 = ObjectIdGetDatum(ownerId);
68
69 if (lomacl != NULL)
70 values[Anum_pg_largeobject_metadata_lomacl - 1]
71 = PointerGetDatum(lomacl);
72 else
73 nulls[Anum_pg_largeobject_metadata_lomacl - 1] = true;
74
75 ntup = heap_form_tuple(RelationGetDescr(pg_lo_meta),
76 values, nulls);
77
78 CatalogTupleInsert(pg_lo_meta, ntup);
79
80 heap_freetuple(ntup);
81
82 table_close(pg_lo_meta, RowExclusiveLock);
83
84 /* dependencies on roles mentioned in default ACL */
85 recordDependencyOnNewAcl(LargeObjectRelationId, loid_new, 0,
86 ownerId, lomacl);
87
88 return loid_new;
89}
90
91/*
92 * Drop a large object having the given LO identifier. Both the data pages
93 * and metadata must be dropped.
94 */
95void
97{
98 Relation pg_lo_meta;
99 Relation pg_largeobject;
100 ScanKeyData skey[1];
101 SysScanDesc scan;
102 HeapTuple tuple;
103
104 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
106
107 pg_largeobject = table_open(LargeObjectRelationId,
109
110 /*
111 * Delete an entry from pg_largeobject_metadata
112 */
113 ScanKeyInit(&skey[0],
114 Anum_pg_largeobject_metadata_oid,
115 BTEqualStrategyNumber, F_OIDEQ,
116 ObjectIdGetDatum(loid));
117
118 scan = systable_beginscan(pg_lo_meta,
119 LargeObjectMetadataOidIndexId, true,
120 NULL, 1, skey);
121
122 tuple = systable_getnext(scan);
123 if (!HeapTupleIsValid(tuple))
125 (errcode(ERRCODE_UNDEFINED_OBJECT),
126 errmsg("large object %u does not exist", loid)));
127
128 CatalogTupleDelete(pg_lo_meta, &tuple->t_self);
129
130 systable_endscan(scan);
131
132 /*
133 * Delete all the associated entries from pg_largeobject
134 */
135 ScanKeyInit(&skey[0],
136 Anum_pg_largeobject_loid,
137 BTEqualStrategyNumber, F_OIDEQ,
138 ObjectIdGetDatum(loid));
139
140 scan = systable_beginscan(pg_largeobject,
141 LargeObjectLOidPNIndexId, true,
142 NULL, 1, skey);
143 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
144 {
145 CatalogTupleDelete(pg_largeobject, &tuple->t_self);
146 }
147
148 systable_endscan(scan);
149
150 table_close(pg_largeobject, RowExclusiveLock);
151
152 table_close(pg_lo_meta, RowExclusiveLock);
153}
154
155/*
156 * LargeObjectExists
157 *
158 * We don't use the system cache for large object metadata, for fear of
159 * using too much local memory.
160 *
161 * This function always scans the system catalog using an up-to-date snapshot,
162 * so it should not be used when a large object is opened in read-only mode
163 * (because large objects opened in read only mode are supposed to be viewed
164 * relative to the caller's snapshot, whereas in read-write mode they are
165 * relative to a current snapshot).
166 */
167bool
169{
170 return LargeObjectExistsWithSnapshot(loid, NULL);
171}
172
173/*
174 * Same as LargeObjectExists(), except snapshot to read with can be specified.
175 */
176bool
178{
179 Relation pg_lo_meta;
180 ScanKeyData skey[1];
181 SysScanDesc sd;
182 HeapTuple tuple;
183 bool retval = false;
184
185 ScanKeyInit(&skey[0],
186 Anum_pg_largeobject_metadata_oid,
187 BTEqualStrategyNumber, F_OIDEQ,
188 ObjectIdGetDatum(loid));
189
190 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
192
193 sd = systable_beginscan(pg_lo_meta,
194 LargeObjectMetadataOidIndexId, true,
195 snapshot, 1, skey);
196
197 tuple = systable_getnext(sd);
198 if (HeapTupleIsValid(tuple))
199 retval = true;
200
202
203 table_close(pg_lo_meta, AccessShareLock);
204
205 return retval;
206}
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:4325
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:4245
static Datum values[MAXATTR]
Definition: bootstrap.c:153
#define OidIsValid(objectId)
Definition: c.h:774
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:448
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:603
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:514
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:388
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
#define AccessShareLock
Definition: lockdefs.h:36
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:469
@ OBJECT_LARGEOBJECT
Definition: parsenodes.h:2347
void LargeObjectDrop(Oid loid)
bool LargeObjectExistsWithSnapshot(Oid loid, Snapshot snapshot)
bool LargeObjectExists(Oid loid)
Oid LargeObjectCreate(Oid loid)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetDescr(relation)
Definition: rel.h:540
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define BTEqualStrategyNumber
Definition: stratnum.h:31
ItemPointerData t_self
Definition: htup.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40