Skip to content

Commit 3978271

Browse files
committed
Disconnect SPI at end of Function_create
Issue #544 presented a mystery where the regression test for trigger transition tables would succeed with check_function_bodies on, but fail with check_function_bodies off. In the 'on' case, the function would be ready to use ahead of its first invocation (the validator having done all the necessary work). The first invocation would find the function cached and invoke it, with SPI not yet connected. On the function's first use of SPI, Invocation_assertConnect sees there is a TriggerData associated with the invocation, and registers the transition tables to make them visible by the expected names. In the 'off' case, the function's first invocation requires Function_create to do its work, which may involve an SPI connection from PL/Java's class loader. Taking place before the invocation has been recognized as handling a trigger, this connection is made without registering the transition tables. By the time the function itself first uses SPI, the TriggerData has been associated with the invocation, but no registration occurs because SPI is already connected. Ending any SPI connection that Function_create may have made ensures that the first use of SPI by the function itself makes a new SPI connection that is appropriately set up. It is a bug for check_function_bodies to have an effect on a function's runtime behavior and not just on its validation. To catch such behavior in the future, the regression test script now repeats the example jar installation and test execution with check_function_bodies set to off.
1 parent 4dc95a0 commit 3978271

3 files changed

Lines changed: 49 additions & 11 deletions

File tree

‎CI/common‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,32 @@ boolean loadExamplesAndTest(Connection c) throws Exception
279279
);
280280
}
281281

282+
/*
283+
* Unloads the PL/Java example and Saxon jars, returning true if that produced
284+
* the expected sequence of results (not necessarily meaning success, unless
285+
* results.get("ng") is also zero afterward.
286+
*/
287+
boolean unloadExamples(Connection c) throws Exception
288+
{
289+
return stateMachine(
290+
"remove example and saxon jars",
291+
null,
292+
293+
Stream.concat(
294+
Node.removeJar(c, "examples", true),
295+
Node.removeJar(c, "saxon", false)
296+
)
297+
.flatMap(Node::semiFlattenDiagnostics)
298+
.peek(Node::peek),
299+
300+
(o,p,q) -> isDiagnostic(o, Set.of("error")) ? 1 : -2,
301+
(o,p,q) -> isVoidResultSet(o, 1, 1) ? 3 : false,
302+
303+
(o,p,q) -> isDiagnostic(o, Set.of("error")) ? 3 : -4,
304+
(o,p,q) -> isVoidResultSet(o, 1, 1) ? 5 : false,
305+
306+
(o,p,q) -> null == o
307+
);
308+
}
309+
282310
int pgMajorVersion;

‎CI/integration‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,27 @@ try (
205205
/*
206206
* Also confirm that the generated undeploy actions work.
207207
*/
208-
succeeding &= stateMachine(
209-
"remove jar void result",
210-
null,
208+
unloadExamples(c);
211209

212-
q(c, "SELECT sqlj.remove_jar('examples', true)")
213-
.flatMap(Node::semiFlattenDiagnostics)
214-
.peek(Node::peek),
210+
/*
211+
* Repeat the regression tests with check_function_bodies off (issue 544).
212+
*/
213+
try ( Connection c2 = n1.connect() )
214+
{
215+
succeeding &= stateMachine(
216+
"check_function_bodies off",
217+
null,
215218

216-
(o,p,q) -> isDiagnostic(o, Set.of("error")) ? 1 : -2,
217-
(o,p,q) -> isVoidResultSet(o, 1, 1) ? 3 : false,
218-
(o,p,q) -> null == o
219-
);
219+
q(c2, "SET check_function_bodies TO off")
220+
.flatMap(Node::semiFlattenDiagnostics)
221+
.peek(Node::peek),
222+
223+
(o,p,q) -> isDiagnostic(o, Set.of("error")) ? 1 : -2,
224+
NOTHING_OR_PGJDBC_ZERO_COUNT,
225+
(o,p,q) -> null == o
226+
);
227+
succeeding &= loadExamplesAndTest(c2) && unloadExamples(c2);
228+
}
220229

221230
/*
222231
* Get another new connection and make sure the extension can be

‎pljava-so/src/main/c/Function.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004-2025 Tada AB and other contributors, as listed below.
2+
* Copyright (c) 2004-2026 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
@@ -789,6 +789,7 @@ static Function Function_create(
789789
funcOid);
790790
}
791791

792+
Invocation_assertDisconnect(); /* func may make its own connection */
792793
return self;
793794
}
794795

0 commit comments

Comments
 (0)