Skip to content

PG-2278 Re-use cipher contexts for SMGR encryption#554

Open
jeltz wants to merge 1 commit intopercona:mainfrom
jeltz:perf-reuse-cbc-ctx
Open

PG-2278 Re-use cipher contexts for SMGR encryption#554
jeltz wants to merge 1 commit intopercona:mainfrom
jeltz:perf-reuse-cbc-ctx

Conversation

@jeltz
Copy link
Copy Markdown
Collaborator

@jeltz jeltz commented Mar 28, 2026

Profiling with perf revealed that EVP_CipherInit_ex() showed up quite a lot when encrypting and decrypting tables, so to decrease how much it shows up we make sure to only allocate and initialize contexts for SMGR encryption once on loading pg_tde and then re-use those contexts but re-initializing them with different keys and IVs on use, which is a lot cheaper than doing a full initialization. This way of calling EVP_CipherInit_ex() is poorly documented in OpenSSL.

WAL encryption already has logic for reusing contexts so this optimization is not as relevant there.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 28, 2026

Codecov Report

❌ Patch coverage is 70.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.44%. Comparing base (6ca54ed) to head (b854657).

❌ Your project status has failed because the head coverage (75.83%) is below the target coverage (90.00%). You can increase the head coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #554      +/-   ##
==========================================
- Coverage   57.47%   57.44%   -0.03%     
==========================================
  Files          68       68              
  Lines       10709    10711       +2     
  Branches     2640     2642       +2     
==========================================
- Hits         6155     6153       -2     
- Misses       3292     3295       +3     
- Partials     1262     1263       +1     
Components Coverage Δ
access 80.67% <ø> (ø)
bin 63.76% <ø> (ø)
catalog 77.86% <ø> (ø)
common 76.47% <ø> (ø)
encryption 57.02% <70.00%> (-0.96%) ⬇️
keyring 65.12% <ø> (ø)
src 87.33% <ø> (ø)
smgr 89.54% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jeltz jeltz force-pushed the perf-reuse-cbc-ctx branch from 40ec44e to 3b567f5 Compare March 28, 2026 23:15
@jeltz jeltz force-pushed the perf-reuse-cbc-ctx branch 3 times, most recently from e2093dc to d25fbe4 Compare March 31, 2026 23:11
@jeltz jeltz changed the title Re-use cipher contexts for SMGR-level encryption PG-2278 Re-use cipher contexts for SMGR encryption Mar 31, 2026
@jeltz jeltz marked this pull request as ready for review March 31, 2026 23:12
@jeltz jeltz force-pushed the perf-reuse-cbc-ctx branch 8 times, most recently from f62f4cb to c9c4232 Compare March 31, 2026 23:31
@jeltz jeltz force-pushed the perf-reuse-cbc-ctx branch 2 times, most recently from 5855d48 to 489ae42 Compare April 2, 2026 15:23
@jeltz jeltz requested a review from dutow April 2, 2026 15:25
Profiling with perf revealed that EVP_CipherInit_ex() showed up quite a
lot when encrypting and decrypting tables, so to decrease how much it
shows up we make sure to only allocate and initialize contexts for SMGR
encryption once on loading pg_tde and then re-use those contexts but
re-initializing them with different keys and IVs on use, which is a lot
cheaper than doing a full initialization. This way of calling
EVP_CipherInit_ex() is poorly documented in OpenSSL.

WAL encryption already has logic for reusing contexts so this
optimization is not as relevant there. Also removes an no longer true
comment related to the WAL encryption.
@jeltz jeltz force-pushed the perf-reuse-cbc-ctx branch from 489ae42 to b854657 Compare April 2, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants