Skip to content

Commit 8aaeb04

Browse files
committed
Accessors in Database and RegClass for new models
ForeignTable is simply represented by two accessors added to RegClass. When foreign-table info is wanted, a little class RegClass holds in a single slot gets instantiated and constructs both values. The slot's invalidation still uses the RegClass switch point, rather than also hooking invalidation for pg_foreign_table. In passing, catch up with two pg_database attributes that changed in PG 15 from name to text.
1 parent cf8aa4c commit 8aaeb04

File tree

7 files changed

+212
-11
lines changed

7 files changed

+212
-11
lines changed

pljava-api/src/main/java/org/postgresql/pljava/model/Database.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2022-2025 Tada AB and other contributors, as listed below.
33
*
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the The BSD 3-Clause License
@@ -56,5 +56,5 @@ public interface Database
5656
// oid lastsysoid
5757
// xid frozenxid
5858
// xid minmxid
59-
// oid tablespace
59+
Tablespace tablespace();
6060
}

pljava-api/src/main/java/org/postgresql/pljava/model/RegClass.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,16 @@ enum ReplicaIdentity { DEFAULT, NOTHING, ALL, INDEX }
8383
* associated with this relation.
8484
*/
8585
RegType ofType();
86-
// am
87-
// filenode
88-
// tablespace
86+
87+
AccessMethod accessMethod();
88+
89+
/* Of limited interest ... used in forming pathname of relation on disk,
90+
* but in very fiddly ways and dependent on the access method.
91+
*
92+
int filenode();
93+
*/
94+
95+
Tablespace tablespace();
8996

9097
/* Of limited interest ... estimates used by planner
9198
*
@@ -129,5 +136,16 @@ enum ReplicaIdentity { DEFAULT, NOTHING, ALL, INDEX }
129136
Map<Simple,String> options();
130137
SQLXML partitionBound();
131138

139+
/**
140+
* The {@link ForeignServer} if this is a foreign table, otherwise null.
141+
*/
142+
ForeignServer foreignServer();
143+
144+
/**
145+
* Table options understood by the {@link #foreignServer foreign server}
146+
* if this is a foreign table, otherwise null.
147+
*/
148+
Map<Simple,String> foreignOptions();
149+
132150
TupleDescriptor.Interned tupleDescriptor();
133151
}

pljava-so/src/main/c/ModelConstants.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <catalog/pg_tablespace.h>
3636
#include <catalog/pg_foreign_data_wrapper.h>
3737
#include <catalog/pg_foreign_server.h>
38+
#include <catalog/pg_foreign_table.h>
3839

3940
#include <commands/trigger.h>
4041

@@ -279,6 +280,7 @@ static int32 constants[] = {
279280
CONSTANT(TABLESPACEOID),
280281
CONSTANT(FOREIGNDATAWRAPPEROID),
281282
CONSTANT(FOREIGNSERVEROID),
283+
CONSTANT(FOREIGNTABLEREL),
282284

283285

284286

@@ -322,6 +324,7 @@ StaticAssertStmt((c) == \
322324
CONFIRMCONST( OperatorRelationId );
323325
CONFIRMCONST( TriggerRelationId );
324326
CONFIRMCONST( ExtensionRelationId );
327+
CONFIRMCONST( ForeignTableRelationId ); /* <-CatalogObjectImpl only */
325328
CONFIRMCONST( CollationRelationId );
326329
CONFIRMCONST( TransformRelationId );
327330
CONFIRMCONST( TSDictionaryRelationId );

pljava/src/main/java/org/postgresql/pljava/pg/CatalogObjectImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,12 @@ static Supplier<CatalogObjectImpl> typeConstructorFor(int oid)
727727
@Native public static final int ANYCOMPATIBLENONARRAYOID = 5079;
728728
@Native public static final int ANYCOMPATIBLERANGEOID = 5080;
729729

730+
/*
731+
* A relation ID that won't be used to construct a full-blown catalog
732+
* object, but used in RegClassImpl.
733+
*/
734+
@Native public static final int ForeignTableRelationId = 3118;
735+
730736
/*
731737
* Indices into arrays used for syscache invalidation callbacks.
732738
* One of these is a boolean native array the C callback can check

pljava/src/main/java/org/postgresql/pljava/pg/DatabaseImpl.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import java.util.function.Function;
2323

24+
import org.postgresql.pljava.Adapter.As;
25+
2426
import org.postgresql.pljava.internal.SwitchPointCache.Builder;
2527
import org.postgresql.pljava.internal.SwitchPointCache.SwitchPoint;
2628

@@ -34,8 +36,10 @@
3436
import static org.postgresql.pljava.pg.adt.NameAdapter.SIMPLE_INSTANCE;
3537
import static org.postgresql.pljava.pg.adt.NameAdapter.AS_STRING_INSTANCE;
3638
import static org.postgresql.pljava.pg.adt.OidAdapter.REGROLE_INSTANCE;
39+
import static org.postgresql.pljava.pg.adt.OidAdapter.TABLESPACE_INSTANCE;
3740
import static org.postgresql.pljava.pg.adt.Primitives.BOOLEAN_INSTANCE;
3841
import static org.postgresql.pljava.pg.adt.Primitives.INT4_INSTANCE;
42+
import org.postgresql.pljava.pg.adt.TextAdapter;
3943

4044
import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Simple;
4145
import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Unqualified;
@@ -49,6 +53,9 @@ class DatabaseImpl extends Addressed<Database>
4953
{
5054
private static final Function<MethodHandle[],MethodHandle[]> s_initializer;
5155

56+
private static final As<String,?> COLLCTYPEADAPTER =
57+
PG_VERSION_NUM >= 150000 ? TextAdapter.INSTANCE : AS_STRING_INSTANCE;
58+
5259
/* Implementation of Addressed */
5360

5461
@Override
@@ -101,6 +108,7 @@ private static List<CatalogObject.Grant> grants(DatabaseImpl o)
101108
static final int SLOT_TEMPLATE;
102109
static final int SLOT_ALLOWCONNECTION;
103110
static final int SLOT_CONNECTIONLIMIT;
111+
static final int SLOT_TABLESPACE;
104112
static final int NSLOTS;
105113

106114
static
@@ -129,6 +137,7 @@ private static List<CatalogObject.Grant> grants(DatabaseImpl o)
129137
.withDependent( "template", SLOT_TEMPLATE = i++)
130138
.withDependent("allowConnection", SLOT_ALLOWCONNECTION = i++)
131139
.withDependent("connectionLimit", SLOT_CONNECTIONLIMIT = i++)
140+
.withDependent( "tablespace", SLOT_TABLESPACE = i++)
132141

133142
.build()
134143
/*
@@ -149,6 +158,7 @@ static class Att
149158
static final Attribute DATISTEMPLATE;
150159
static final Attribute DATALLOWCONN;
151160
static final Attribute DATCONNLIMIT;
161+
static final Attribute DATTABLESPACE;
152162

153163
static
154164
{
@@ -161,7 +171,8 @@ static class Att
161171
"datctype",
162172
"datistemplate",
163173
"datallowconn",
164-
"datconnlimit"
174+
"datconnlimit",
175+
"dattablespace"
165176
).iterator();
166177

167178
DATNAME = itr.next();
@@ -173,6 +184,7 @@ static class Att
173184
DATISTEMPLATE = itr.next();
174185
DATALLOWCONN = itr.next();
175186
DATCONNLIMIT = itr.next();
187+
DATTABLESPACE = itr.next();
176188

177189
assert ! itr.hasNext() : "attribute initialization miscount";
178190
}
@@ -189,13 +201,13 @@ private static CharsetEncoding encoding(DatabaseImpl o) throws SQLException
189201
private static String collate(DatabaseImpl o) throws SQLException
190202
{
191203
TupleTableSlot s = o.cacheTuple();
192-
return s.get(Att.DATCOLLATE, AS_STRING_INSTANCE);
204+
return s.get(Att.DATCOLLATE, COLLCTYPEADAPTER);
193205
}
194206

195207
private static String ctype(DatabaseImpl o) throws SQLException
196208
{
197209
TupleTableSlot s = o.cacheTuple();
198-
return s.get(Att.DATCTYPE, AS_STRING_INSTANCE);
210+
return s.get(Att.DATCTYPE, COLLCTYPEADAPTER);
199211
}
200212

201213
private static boolean template(DatabaseImpl o) throws SQLException
@@ -216,6 +228,12 @@ private static int connectionLimit(DatabaseImpl o) throws SQLException
216228
return s.get(Att.DATCONNLIMIT, INT4_INSTANCE);
217229
}
218230

231+
private static Tablespace tablespace(DatabaseImpl o) throws SQLException
232+
{
233+
TupleTableSlot s = o.cacheTuple();
234+
return s.get(Att.DATTABLESPACE, TABLESPACE_INSTANCE);
235+
}
236+
219237
/* API methods */
220238

221239
@Override
@@ -301,4 +319,18 @@ public int connectionLimit()
301319
throw unchecked(t);
302320
}
303321
}
322+
323+
@Override
324+
public Tablespace tablespace()
325+
{
326+
try
327+
{
328+
MethodHandle h = m_slots[SLOT_TABLESPACE];
329+
return (Tablespace)h.invokeExact(this, h);
330+
}
331+
catch ( Throwable t )
332+
{
333+
throw unchecked(t);
334+
}
335+
}
304336
}

pljava/src/main/java/org/postgresql/pljava/pg/ModelConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ public void close()
376376

377377
@Native private static final int IDX_FOREIGNDATAWRAPPEROID = 519;
378378
@Native private static final int IDX_FOREIGNSERVEROID = 520;
379+
@Native private static final int IDX_FOREIGNTABLEREL = 521;
379380

380381

381382

@@ -573,6 +574,7 @@ public void close()
573574

574575
public static final int FOREIGNDATAWRAPPEROID; // ForeignDataWrapperImpl
575576
public static final int FOREIGNSERVEROID; // ForeignServerImpl
577+
public static final int FOREIGNTABLEREL; // RegClassImpl
576578

577579

578580

@@ -772,6 +774,7 @@ public void close()
772774

773775
FOREIGNDATAWRAPPEROID = n.get(IDX_FOREIGNDATAWRAPPEROID);
774776
FOREIGNSERVEROID = n.get(IDX_FOREIGNSERVEROID);
777+
FOREIGNTABLEREL = n.get(IDX_FOREIGNTABLEREL);
775778

776779

777780

0 commit comments

Comments
 (0)