Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 412229d

Browse files
committed
Deduplicate code in LargeObjectExists and myLargeObjectExists.
myLargeObjectExists() and LargeObjectExists() had nearly identical code, except for handling snapshots. This commit renames myLargeObjectExists() to LargeObjectExistsWithSnapshot() and refactors LargeObjectExists() to call it internally, reducing duplication. Author: Yugo Nagata Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/20240702163444.ab586f6075e502eb84f11b1a@sranhm.sraoss.co.jp
1 parent 23d0b48 commit 412229d

File tree

3 files changed

+13
-42
lines changed

3 files changed

+13
-42
lines changed

src/backend/catalog/pg_largeobject.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include "access/genam.h"
18-
#include "access/htup_details.h"
1917
#include "access/table.h"
2018
#include "catalog/catalog.h"
2119
#include "catalog/indexing.h"
@@ -153,6 +151,15 @@ LargeObjectDrop(Oid loid)
153151
*/
154152
bool
155153
LargeObjectExists(Oid loid)
154+
{
155+
return LargeObjectExistsWithSnapshot(loid, NULL);
156+
}
157+
158+
/*
159+
* Same as LargeObjectExists(), except snapshot to read with can be specified.
160+
*/
161+
bool
162+
LargeObjectExistsWithSnapshot(Oid loid, Snapshot snapshot)
156163
{
157164
Relation pg_lo_meta;
158165
ScanKeyData skey[1];
@@ -170,7 +177,7 @@ LargeObjectExists(Oid loid)
170177

171178
sd = systable_beginscan(pg_lo_meta,
172179
LargeObjectMetadataOidIndexId, true,
173-
NULL, 1, skey);
180+
snapshot, 1, skey);
174181

175182
tuple = systable_getnext(sd);
176183
if (HeapTupleIsValid(tuple))

src/backend/storage/large_object/inv_api.c

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "catalog/indexing.h"
4242
#include "catalog/objectaccess.h"
4343
#include "catalog/pg_largeobject.h"
44-
#include "catalog/pg_largeobject_metadata.h"
4544
#include "libpq/libpq-fs.h"
4645
#include "miscadmin.h"
4746
#include "storage/large_object.h"
@@ -123,43 +122,6 @@ close_lo_relation(bool isCommit)
123122
}
124123

125124

126-
/*
127-
* Same as pg_largeobject.c's LargeObjectExists(), except snapshot to
128-
* read with can be specified.
129-
*/
130-
static bool
131-
myLargeObjectExists(Oid loid, Snapshot snapshot)
132-
{
133-
Relation pg_lo_meta;
134-
ScanKeyData skey[1];
135-
SysScanDesc sd;
136-
HeapTuple tuple;
137-
bool retval = false;
138-
139-
ScanKeyInit(&skey[0],
140-
Anum_pg_largeobject_metadata_oid,
141-
BTEqualStrategyNumber, F_OIDEQ,
142-
ObjectIdGetDatum(loid));
143-
144-
pg_lo_meta = table_open(LargeObjectMetadataRelationId,
145-
AccessShareLock);
146-
147-
sd = systable_beginscan(pg_lo_meta,
148-
LargeObjectMetadataOidIndexId, true,
149-
snapshot, 1, skey);
150-
151-
tuple = systable_getnext(sd);
152-
if (HeapTupleIsValid(tuple))
153-
retval = true;
154-
155-
systable_endscan(sd);
156-
157-
table_close(pg_lo_meta, AccessShareLock);
158-
159-
return retval;
160-
}
161-
162-
163125
/*
164126
* Extract data field from a pg_largeobject tuple, detoasting if needed
165127
* and verifying that the length is sane. Returns data pointer (a bytea *),
@@ -279,7 +241,7 @@ inv_open(Oid lobjId, int flags, MemoryContext mcxt)
279241
snapshot = GetActiveSnapshot();
280242

281243
/* Can't use LargeObjectExists here because we need to specify snapshot */
282-
if (!myLargeObjectExists(lobjId, snapshot))
244+
if (!LargeObjectExistsWithSnapshot(lobjId, snapshot))
283245
ereport(ERROR,
284246
(errcode(ERRCODE_UNDEFINED_OBJECT),
285247
errmsg("large object %u does not exist", lobjId)));

src/include/catalog/pg_largeobject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "catalog/genbki.h"
2222
#include "catalog/pg_largeobject_d.h"
23+
#include "utils/snapshot.h"
2324

2425
/* ----------------
2526
* pg_largeobject definition. cpp turns this into
@@ -49,5 +50,6 @@ DECLARE_UNIQUE_INDEX_PKEY(pg_largeobject_loid_pn_index, 2683, LargeObjectLOidPNI
4950
extern Oid LargeObjectCreate(Oid loid);
5051
extern void LargeObjectDrop(Oid loid);
5152
extern bool LargeObjectExists(Oid loid);
53+
extern bool LargeObjectExistsWithSnapshot(Oid loid, Snapshot snapshot);
5254

5355
#endif /* PG_LARGEOBJECT_H */

0 commit comments

Comments
 (0)