Skip to content

fix(plisql): refresh package-dependent caches after discard - #1453

Open
Z95-cmyk wants to merge 1 commit into
IvorySQL:masterfrom
Z95-cmyk:fix-package-cache-after-discard
Open

Z95-cmyk wants to merge 1 commit into
IvorySQL:masterfrom
Z95-cmyk:fix-package-cache-after-discard

Conversation

@Z95-cmyk

@Z95-cmyk Z95-cmyk commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • invalidate cached PL/iSQL functions that depend on a discarded package
  • refresh stale parent-function pointers from the package cache before reusing package subprogram metadata
  • clear package cache dependency lists after unlinking them
  • add focused regression coverage for cached standalone callers and prepared package calls across repeated DISCARD PACKAGE operations

This correctness fix was split from #1422 at the maintainer’s request so the crash fix can be reviewed independently from the performance optimization.

Fixes #1452

Verification

  • make -C src/oracle_test/modules/test_package_reset oracle-check — 5/5 tests passed
  • ran the focused regression against the performance-only branch before applying this fix: the backend terminated with signal 11 on the first cached standalone call after DISCARD PACKAGE
  • ran the same regression on this branch: it passes, including repeated discard and prepared-call coverage

AI assistance disclosure

This contribution was prepared with OpenAI Codex using GPT-5. The agent assisted with source and test inspection, issue scoping, drafting code and tests, running verification, reproducing the pre-fix crash, and preparing the PR description. I directed the scope, reviewed the resulting changes against the source and test results, and remain responsible for the submission.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed package-backed functions becoming invalid after repeated DISCARD PACKAGE operations.
    • Preserved correct execution of cached callers and prepared statements after package cache invalidation.
    • Improved reliability of package function refreshes and execution plans following cache resets.
  • Tests

    • Added regression coverage for package function caching, prepared execution, and repeated package discards.

@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The package cache cleanup paths now invalidate dependent functions and clear stale relation lists. Package-backed FuncExpr references refresh from the current cache entry before metadata access. A new regression test covers cached callers, prepared plans, and repeated DISCARD PACKAGE operations.

Changes

Package cache safety

Layer / File(s) Summary
Package teardown invalidation
src/pl/plisql/src/pl_package.c
Dependent functions invalidate fn_xmin before deletion, and removed package-cache relation lists are cleared.
Cached package function refresh
src/pl/plisql/src/pl_package.c
Package-origin FuncExpr references refresh their current cached function before subprogram metadata is read.
Repeated discard regression test
src/oracle_test/modules/test_package_reset/sql/*, src/oracle_test/modules/test_package_reset/expected/*, src/oracle_test/modules/test_package_reset/Makefile
Adds package, caller, prepared-plan, repeated-discard, cleanup, expected-output, and regression-target coverage.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main fix: refreshing package-dependent caches after DISCARD PACKAGE.
Linked Issues check ✅ Passed The code and regression test match #1452 by invalidating stale dependent functions, refreshing package FuncExpr state, clearing dependency links, and covering repeated DISCARD PACKAGE.
Out of Scope Changes check ✅ Passed The changes stay within the crash fix and regression coverage for DISCARD PACKAGE, with no unrelated code paths introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql`:
- Around line 25-26: Replace each EXPLAIN ANALYZE wrapper around EXECUTE
discard_package_call in
src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql
at lines 25-26, 31-32, and 37-38 with a direct EXECUTE discard_package_call;
statement so the returned value is verified. Update the corresponding expected
output in test_discard_function_cache.out to assert the returned row values,
including 42, instead of query-plan output.

In `@src/pl/plisql/src/pl_package.c`:
- Line 2499: Before resetting func->pkgcachelist, call list_free on the existing
list to release its cells, then assign NIL as the reset value. Update the
cleanup logic containing func->pkgcachelist and preserve the existing reset
behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 139ec27b-4f9f-485a-bbc8-fac78297a9f7

📥 Commits

Reviewing files that changed from the base of the PR and between 15d3b5b and 25fc51b.

📒 Files selected for processing (4)
  • src/oracle_test/modules/test_package_reset/Makefile
  • src/oracle_test/modules/test_package_reset/expected/test_discard_function_cache.out
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql
  • src/pl/plisql/src/pl_package.c

Comment on lines +25 to +26
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)
EXECUTE discard_package_call;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Remove EXPLAIN ANALYZE to verify actual query results.

Wrapping the EXECUTE statement in EXPLAIN ANALYZE executes the plan but discards the query result from the output. To verify that the cached function correctly evaluates to 42 after DISCARD PACKAGE without silently returning null or garbage, simply EXECUTE the statement and assert the returned value.

As per path instructions, review tests for the correctness of query results. Note that applying this change will require updating the expected output in test_discard_function_cache.out to match the returned row values instead of the query plan.

  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L25-L26: replace EXPLAIN (ANALYZE, ...) and the execution with just EXECUTE discard_package_call;
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L31-L32: replace EXPLAIN (ANALYZE, ...) and the execution with just EXECUTE discard_package_call;
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L37-L38: replace EXPLAIN (ANALYZE, ...) and the execution with just EXECUTE discard_package_call;
📍 Affects 1 file
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L25-L26 (this comment)
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L31-L32
  • src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql#L37-L38
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql`
around lines 25 - 26, Replace each EXPLAIN ANALYZE wrapper around EXECUTE
discard_package_call in
src/oracle_test/modules/test_package_reset/sql/test_discard_function_cache.sql
at lines 25-26, 31-32, and 37-38 with a direct EXECUTE discard_package_call;
statement so the returned value is verified. Update the corresponding expected
output in test_discard_function_cache.out to assert the returned row values,
including 42, instead of query-plan output.

Source: Path instructions


plisql_remove_function_references(func, refcache);
}
func->pkgcachelist = NIL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Free the list before resetting to avoid a memory leak.

Setting the pkgcachelist to NIL abandons the memory allocated for the List and its cells. In PostgreSQL, this results in a memory leak if the list was allocated in a long-lived context. Use list_free to release the memory before resetting the pointer.

♻️ Proposed fix
-	func->pkgcachelist = NIL;
+	list_free(func->pkgcachelist);
+	func->pkgcachelist = NIL;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func->pkgcachelist = NIL;
list_free(func->pkgcachelist);
func->pkgcachelist = NIL;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/pl/plisql/src/pl_package.c` at line 2499, Before resetting
func->pkgcachelist, call list_free on the existing list to release its cells,
then assign NIL as the reset value. Update the cleanup logic containing
func->pkgcachelist and preserve the existing reset behavior.

@hs-liuxh

Copy link
Copy Markdown
Collaborator

Thanks for your work! This fix is crucial for IvorySQL's stability. We'll discuss it further in our review meeting.

@hs-liuxh hs-liuxh self-assigned this Aug 3, 2026
@github-actions github-actions Bot added the waiting-on-author Author has not responded to review comments or @mentions for a long time label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Hi @Z95-cmyk,

This pull request has been waiting for your response for about 106 days.

Reason: Unresolved review comment exists (waiting about 106 days)

Please:

  • reply to the pending review comments / conversation, or
  • push updates addressing the feedback, or
  • let us know if you need more time / want to close this PR.

This is an automated reminder. The label waiting-on-author was added so maintainers can track it.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on-author Author has not responded to review comments or @mentions for a long time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DISCARD PACKAGE can crash cached PL/iSQL callers

2 participants