PostgreSQL Source Code  git master
pg_largeobject.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "catalog/catalog.h"
#include "catalog/indexing.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "miscadmin.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 37 of file pg_largeobject.c.

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 
45  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
47 
48  /*
49  * Insert metadata of the largeobject
50  */
51  memset(values, 0, sizeof(values));
52  memset(nulls, false, sizeof(nulls));
53 
54  if (OidIsValid(loid))
55  loid_new = loid;
56  else
57  loid_new = GetNewOidWithIndex(pg_lo_meta,
58  LargeObjectMetadataOidIndexId,
59  Anum_pg_largeobject_metadata_oid);
60 
61  values[Anum_pg_largeobject_metadata_oid - 1] = ObjectIdGetDatum(loid_new);
62  values[Anum_pg_largeobject_metadata_lomowner - 1]
64  nulls[Anum_pg_largeobject_metadata_lomacl - 1] = true;
65 
66  ntup = heap_form_tuple(RelationGetDescr(pg_lo_meta),
67  values, nulls);
68 
69  CatalogTupleInsert(pg_lo_meta, ntup);
70 
71  heap_freetuple(ntup);
72 
73  table_close(pg_lo_meta, RowExclusiveLock);
74 
75  return loid_new;
76 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define OidIsValid(objectId)
Definition: c.h:775
Oid GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
Definition: catalog.c:391
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:233
#define RowExclusiveLock
Definition: lockdefs.h:38
Oid GetUserId(void)
Definition: miscinit.c:514
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
#define RelationGetDescr(relation)
Definition: rel.h:531
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40

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

Referenced by inv_create().

◆ LargeObjectDrop()

void LargeObjectDrop ( Oid  loid)

Definition at line 83 of file pg_largeobject.c.

84 {
85  Relation pg_lo_meta;
86  Relation pg_largeobject;
87  ScanKeyData skey[1];
88  SysScanDesc scan;
89  HeapTuple tuple;
90 
91  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
93 
94  pg_largeobject = table_open(LargeObjectRelationId,
96 
97  /*
98  * Delete an entry from pg_largeobject_metadata
99  */
100  ScanKeyInit(&skey[0],
101  Anum_pg_largeobject_metadata_oid,
102  BTEqualStrategyNumber, F_OIDEQ,
103  ObjectIdGetDatum(loid));
104 
105  scan = systable_beginscan(pg_lo_meta,
106  LargeObjectMetadataOidIndexId, true,
107  NULL, 1, skey);
108 
109  tuple = systable_getnext(scan);
110  if (!HeapTupleIsValid(tuple))
111  ereport(ERROR,
112  (errcode(ERRCODE_UNDEFINED_OBJECT),
113  errmsg("large object %u does not exist", loid)));
114 
115  CatalogTupleDelete(pg_lo_meta, &tuple->t_self);
116 
117  systable_endscan(scan);
118 
119  /*
120  * Delete all the associated entries from pg_largeobject
121  */
122  ScanKeyInit(&skey[0],
123  Anum_pg_largeobject_loid,
124  BTEqualStrategyNumber, F_OIDEQ,
125  ObjectIdGetDatum(loid));
126 
127  scan = systable_beginscan(pg_largeobject,
128  LargeObjectLOidPNIndexId, true,
129  NULL, 1, skey);
130  while (HeapTupleIsValid(tuple = systable_getnext(scan)))
131  {
132  CatalogTupleDelete(pg_largeobject, &tuple->t_self);
133  }
134 
135  systable_endscan(scan);
136 
137  table_close(pg_largeobject, RowExclusiveLock);
138 
139  table_close(pg_lo_meta, RowExclusiveLock);
140 }
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:596
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:503
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:384
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:365
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

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

Referenced by doDeletion().

◆ LargeObjectExists()

bool LargeObjectExists ( Oid  loid)

Definition at line 155 of file pg_largeobject.c.

156 {
157  Relation pg_lo_meta;
158  ScanKeyData skey[1];
159  SysScanDesc sd;
160  HeapTuple tuple;
161  bool retval = false;
162 
163  ScanKeyInit(&skey[0],
164  Anum_pg_largeobject_metadata_oid,
165  BTEqualStrategyNumber, F_OIDEQ,
166  ObjectIdGetDatum(loid));
167 
168  pg_lo_meta = table_open(LargeObjectMetadataRelationId,
170 
171  sd = systable_beginscan(pg_lo_meta,
172  LargeObjectMetadataOidIndexId, true,
173  NULL, 1, skey);
174 
175  tuple = systable_getnext(sd);
176  if (HeapTupleIsValid(tuple))
177  retval = true;
178 
179  systable_endscan(sd);
180 
181  table_close(pg_lo_meta, AccessShareLock);
182 
183  return retval;
184 }
#define AccessShareLock
Definition: lockdefs.h:36

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

Referenced by get_object_address(), getObjectDescription(), getObjectIdentityParts(), and objectNamesToOids().