PostgreSQL Source Code  git master
pg_largeobject.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "miscadmin.h"
#include "utils/acl.h"
#include "utils/fmgroids.h"
#include "utils/rel.h"
Include dependency graph for pg_largeobject.c:

Go to the source code of this file.

Functions

Oid LargeObjectCreate (Oid loid)
 
void LargeObjectDrop (Oid loid)
 
bool LargeObjectExists (Oid loid)
 

Function Documentation

◆ LargeObjectCreate()

Oid LargeObjectCreate ( Oid  loid)

Definition at line 40 of file pg_largeobject.c.

References CatalogTupleInsert(), GetNewOidWithIndex(), GetUserId(), heap_form_tuple(), heap_freetuple(), LargeObjectMetadataOidIndexId, ObjectIdGetDatum, OidIsValid, RelationGetDescr, RowExclusiveLock, table_close(), table_open(), and values.

Referenced by inv_create().

41 {
42  Relation pg_lo_meta;
43  HeapTuple ntup;
44  Oid loid_new;
45  Datum values[Natts_pg_largeobject_metadata];
46  bool nulls[Natts_pg_largeobject_metadata];
47 
48  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
50 
51  /*
52  * Insert metadata of the largeobject
53  */
54  memset(values, 0, sizeof(values));
55  memset(nulls, false, sizeof(nulls));
56 
57  if (OidIsValid(loid))
58  loid_new = loid;
59  else
60  loid_new = GetNewOidWithIndex(pg_lo_meta,
62  Anum_pg_largeobject_metadata_oid);
63 
64  values[Anum_pg_largeobject_metadata_oid - 1] = ObjectIdGetDatum(loid_new);
65  values[Anum_pg_largeobject_metadata_lomowner - 1]
67  nulls[Anum_pg_largeobject_metadata_lomacl - 1] = true;
68 
69  ntup = heap_form_tuple(RelationGetDescr(pg_lo_meta),
70  values, nulls);
71 
72  CatalogTupleInsert(pg_lo_meta, ntup);
73 
74  heap_freetuple(ntup);
75 
76  table_close(pg_lo_meta, RowExclusiveLock);
77 
78  return loid_new;
79 }
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:323
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
#define RelationGetDescr(relation)
Definition: rel.h:445
Oid GetUserId(void)
Definition: miscinit.c:380
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:638
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define RowExclusiveLock
Definition: lockdefs.h:38
#define LargeObjectMetadataOidIndexId
Definition: indexing.h:187
uintptr_t Datum
Definition: postgres.h:367
static Datum values[MAXATTR]
Definition: bootstrap.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183

◆ LargeObjectDrop()

void LargeObjectDrop ( Oid  loid)

Definition at line 86 of file pg_largeobject.c.

References BTEqualStrategyNumber, CatalogTupleDelete(), ereport, errcode(), errmsg(), ERROR, HeapTupleIsValid, LargeObjectLOidPNIndexId, LargeObjectMetadataOidIndexId, ObjectIdGetDatum, RowExclusiveLock, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), and table_open().

Referenced by doDeletion().

87 {
88  Relation pg_lo_meta;
89  Relation pg_largeobject;
90  ScanKeyData skey[1];
91  SysScanDesc scan;
92  HeapTuple tuple;
93 
94  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
96 
97  pg_largeobject = table_open(LargeObjectRelationId,
99 
100  /*
101  * Delete an entry from pg_largeobject_metadata
102  */
103  ScanKeyInit(&skey[0],
104  Anum_pg_largeobject_metadata_oid,
105  BTEqualStrategyNumber, F_OIDEQ,
106  ObjectIdGetDatum(loid));
107 
108  scan = systable_beginscan(pg_lo_meta,
110  NULL, 1, skey);
111 
112  tuple = systable_getnext(scan);
113  if (!HeapTupleIsValid(tuple))
114  ereport(ERROR,
115  (errcode(ERRCODE_UNDEFINED_OBJECT),
116  errmsg("large object %u does not exist", loid)));
117 
118  CatalogTupleDelete(pg_lo_meta, &tuple->t_self);
119 
120  systable_endscan(scan);
121 
122  /*
123  * Delete all the associated entries from pg_largeobject
124  */
125  ScanKeyInit(&skey[0],
126  Anum_pg_largeobject_loid,
127  BTEqualStrategyNumber, F_OIDEQ,
128  ObjectIdGetDatum(loid));
129 
130  scan = systable_beginscan(pg_largeobject,
132  NULL, 1, skey);
133  while (HeapTupleIsValid(tuple = systable_getnext(scan)))
134  {
135  CatalogTupleDelete(pg_largeobject, &tuple->t_self);
136  }
137 
138  systable_endscan(scan);
139 
140  table_close(pg_largeobject, RowExclusiveLock);
141 
142  table_close(pg_lo_meta, RowExclusiveLock);
143 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
int errcode(int sqlerrcode)
Definition: elog.c:570
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define LargeObjectLOidPNIndexId
Definition: indexing.h:184
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ereport(elevel, rest)
Definition: elog.h:141
#define LargeObjectMetadataOidIndexId
Definition: indexing.h:187
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:784
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ LargeObjectExists()

bool LargeObjectExists ( Oid  loid)

Definition at line 158 of file pg_largeobject.c.

References AccessShareLock, BTEqualStrategyNumber, HeapTupleIsValid, LargeObjectMetadataOidIndexId, ObjectIdGetDatum, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), and table_open().

Referenced by get_object_address(), and objectNamesToOids().

159 {
160  Relation pg_lo_meta;
161  ScanKeyData skey[1];
162  SysScanDesc sd;
163  HeapTuple tuple;
164  bool retval = false;
165 
166  ScanKeyInit(&skey[0],
167  Anum_pg_largeobject_metadata_oid,
168  BTEqualStrategyNumber, F_OIDEQ,
169  ObjectIdGetDatum(loid));
170 
171  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
173 
174  sd = systable_beginscan(pg_lo_meta,
176  NULL, 1, skey);
177 
178  tuple = systable_getnext(sd);
179  if (HeapTupleIsValid(tuple))
180  retval = true;
181 
182  systable_endscan(sd);
183 
184  table_close(pg_lo_meta, AccessShareLock);
185 
186  return retval;
187 }
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define AccessShareLock
Definition: lockdefs.h:36
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define LargeObjectMetadataOidIndexId
Definition: indexing.h:187
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
#define BTEqualStrategyNumber
Definition: stratnum.h:31