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/table.h"
18#include "catalog/catalog.h"
19#include "catalog/indexing.h"
22#include "miscadmin.h"
23#include "utils/acl.h"
24#include "utils/fmgroids.h"
25#include "utils/rel.h"
26
27
28/*
29 * Create a large object having the given LO identifier.
30 *
31 * We create a new large object by inserting an entry into
32 * pg_largeobject_metadata without any data pages, so that the object
33 * will appear to exist with size 0.
34 */
35Oid
37{
38 Relation pg_lo_meta;
39 HeapTuple ntup;
40 Oid loid_new;
41 Datum values[Natts_pg_largeobject_metadata];
42 bool nulls[Natts_pg_largeobject_metadata];
43 Oid ownerId;
44 Acl *lomacl;
45
46 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
48
49 /*
50 * Insert metadata of the largeobject
51 */
52 memset(values, 0, sizeof(values));
53 memset(nulls, false, sizeof(nulls));
54
55 if (OidIsValid(loid))
56 loid_new = loid;
57 else
58 loid_new = GetNewOidWithIndex(pg_lo_meta,
59 LargeObjectMetadataOidIndexId,
60 Anum_pg_largeobject_metadata_oid);
61 ownerId = GetUserId();
63
64 values[Anum_pg_largeobject_metadata_oid - 1] = ObjectIdGetDatum(loid_new);
65 values[Anum_pg_largeobject_metadata_lomowner - 1]
66 = ObjectIdGetDatum(ownerId);
67
68 if (lomacl != NULL)
69 values[Anum_pg_largeobject_metadata_lomacl - 1]
70 = PointerGetDatum(lomacl);
71 else
72 nulls[Anum_pg_largeobject_metadata_lomacl - 1] = true;
73
74 ntup = heap_form_tuple(RelationGetDescr(pg_lo_meta),
75 values, nulls);
76
77 CatalogTupleInsert(pg_lo_meta, ntup);
78
79 heap_freetuple(ntup);
80
81 table_close(pg_lo_meta, RowExclusiveLock);
82
83 /* dependencies on roles mentioned in default ACL */
84 recordDependencyOnNewAcl(LargeObjectRelationId, loid_new, 0,
85 ownerId, lomacl);
86
87 return loid_new;
88}
89
90/*
91 * Drop a large object having the given LO identifier. Both the data pages
92 * and metadata must be dropped.
93 */
94void
96{
97 Relation pg_lo_meta;
98 Relation pg_largeobject;
99 ScanKeyData skey[1];
100 SysScanDesc scan;
101 HeapTuple tuple;
102
103 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
105
106 pg_largeobject = table_open(LargeObjectRelationId,
108
109 /*
110 * Delete an entry from pg_largeobject_metadata
111 */
112 ScanKeyInit(&skey[0],
113 Anum_pg_largeobject_metadata_oid,
114 BTEqualStrategyNumber, F_OIDEQ,
115 ObjectIdGetDatum(loid));
116
117 scan = systable_beginscan(pg_lo_meta,
118 LargeObjectMetadataOidIndexId, true,
119 NULL, 1, skey);
120
121 tuple = systable_getnext(scan);
122 if (!HeapTupleIsValid(tuple))
124 (errcode(ERRCODE_UNDEFINED_OBJECT),
125 errmsg("large object %u does not exist", loid)));
126
127 CatalogTupleDelete(pg_lo_meta, &tuple->t_self);
128
129 systable_endscan(scan);
130
131 /*
132 * Delete all the associated entries from pg_largeobject
133 */
134 ScanKeyInit(&skey[0],
135 Anum_pg_largeobject_loid,
136 BTEqualStrategyNumber, F_OIDEQ,
137 ObjectIdGetDatum(loid));
138
139 scan = systable_beginscan(pg_largeobject,
140 LargeObjectLOidPNIndexId, true,
141 NULL, 1, skey);
142 while (HeapTupleIsValid(tuple = systable_getnext(scan)))
143 {
144 CatalogTupleDelete(pg_largeobject, &tuple->t_self);
145 }
146
147 systable_endscan(scan);
148
149 table_close(pg_largeobject, RowExclusiveLock);
150
151 table_close(pg_lo_meta, RowExclusiveLock);
152}
153
154/*
155 * LargeObjectExists
156 *
157 * We don't use the system cache for large object metadata, for fear of
158 * using too much local memory.
159 *
160 * This function always scans the system catalog using an up-to-date snapshot,
161 * so it should not be used when a large object is opened in read-only mode
162 * (because large objects opened in read only mode are supposed to be viewed
163 * relative to the caller's snapshot, whereas in read-write mode they are
164 * relative to a current snapshot).
165 */
166bool
168{
169 return LargeObjectExistsWithSnapshot(loid, NULL);
170}
171
172/*
173 * Same as LargeObjectExists(), except snapshot to read with can be specified.
174 */
175bool
177{
178 Relation pg_lo_meta;
179 ScanKeyData skey[1];
180 SysScanDesc sd;
181 HeapTuple tuple;
182 bool retval = false;
183
184 ScanKeyInit(&skey[0],
185 Anum_pg_largeobject_metadata_oid,
186 BTEqualStrategyNumber, F_OIDEQ,
187 ObjectIdGetDatum(loid));
188
189 pg_lo_meta = table_open(LargeObjectMetadataRelationId,
191
192 sd = systable_beginscan(pg_lo_meta,
193 LargeObjectMetadataOidIndexId, true,
194 snapshot, 1, skey);
195
196 tuple = systable_getnext(sd);
197 if (HeapTupleIsValid(tuple))
198 retval = true;
199
201
202 table_close(pg_lo_meta, AccessShareLock);
203
204 return retval;
205}
void recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
Definition: aclchk.c:4312
Acl * get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
Definition: aclchk.c:4232
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define OidIsValid(objectId)
Definition: c.h:746
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:450
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:149
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:520
@ OBJECT_LARGEOBJECT
Definition: parsenodes.h:2339
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:327
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
#define RelationGetDescr(relation)
Definition: rel.h:542
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