Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions .github/workflows/publish-manual-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ on:
permissions:
contents: read

# Not the certified publisher's group: a queued manual run would replace a
# pending certified publication. The latest-release check right before
# moving :latest keeps the two from overwriting each other's image.
concurrency:
group: manual-release-docker
cancel-in-progress: false
Expand Down Expand Up @@ -70,13 +73,28 @@ jobs:
password: ${{ github.token }}
- name: Publish version and latest tags
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ inputs.tag }}
LINUXSERVER_AMD64: ${{ steps.source.outputs.linuxserver_amd64 }}
LINUXSERVER_ARM64: ${{ steps.source.outputs.linuxserver_arm64 }}
PLEXINC_AMD64: ${{ steps.source.outputs.plexinc_amd64 }}
PLEXINC_ARM64: ${{ steps.source.outputs.plexinc_arm64 }}
run: |
set -eu
# :latest follows the repository's latest GitHub release, so a
# manual publication of an older or newer-but-unreleased tag never
# replaces the image of the current (possibly certified) release.
# Decided before any tag is written (an API error fails the run
# while it can still be rerun), and only while no certified
# publication is running, which would set :latest itself.
latest_release=$(gh release list --repo "$GITHUB_REPOSITORY" --exclude-drafts --exclude-pre-releases \
--limit 20 --json tagName,isLatest --jq '[.[] | select(.isLatest)][0].tagName // ""')
certified_publications=$(gh run list --repo "$GITHUB_REPOSITORY" --workflow publish-certified-release.yml \
--limit 10 --json status --jq '[.[] | select(.status != "completed")] | length')
move_latest=false
if [[ "$latest_release" == "$TAG" && "$certified_publications" == 0 ]]; then
move_latest=true
fi
publish() {
local repository=$1 amd64=$2 arm64=$3
if docker buildx imagetools inspect "$repository:$TAG" >/dev/null 2>&1; then
Expand All @@ -86,8 +104,12 @@ jobs:
docker buildx imagetools create --tag "$repository:$TAG" "$amd64" "$arm64"
digest=$(docker buildx imagetools inspect "$repository:$TAG" | awk '/^Digest:/ {print $2; exit}')
[[ "$digest" =~ ^sha256:[0-9a-f]{64}$ ]]
docker buildx imagetools create --tag "$repository:latest" "$repository@$digest"
echo "$repository:$TAG -> $digest" >> "$GITHUB_STEP_SUMMARY"
if [[ "$move_latest" == true ]]; then
docker buildx imagetools create --tag "$repository:latest" "$repository@$digest"
echo "$repository:$TAG and :latest -> $digest" >> "$GITHUB_STEP_SUMMARY"
else
echo "$repository:$TAG -> $digest (:latest unchanged; latest release ${latest_release:-none})" >> "$GITHUB_STEP_SUMMARY"
fi
}
publish ghcr.io/cgnl/plex-postgresql-linuxserver "$LINUXSERVER_AMD64" "$LINUXSERVER_ARM64"
publish ghcr.io/cgnl/plex-postgresql-plexinc "$PLEXINC_AMD64" "$PLEXINC_ARM64"
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- A read that fails while PostgreSQL executes it now reports the error. Reads that do not stream (for example `LIMIT 1` queries) returned an empty result instead, with only a log line.
- `ROLLBACK` after the PostgreSQL session was lost mid-transaction clears the transaction; the handle could not start a new transaction until it was closed. It succeeds, unless the session broke during `COMMIT`: then it reports that the commit's outcome is unknown. Closed or destroyed connections no longer leave transaction state behind for a later connection at the same address.
- A connection error noted during one step no longer makes the next failing statement on the same thread retry as if its connection had failed.
- The result-description cache tracks uncommitted DDL per session: it resumes after DDL that ran outside a transaction, and another session's commit or autocommit write (or an inner `RELEASE SAVEPOINT`) no longer ends the window early.
- A statement whose stream was read ahead is no longer re-run from the first row when it is stepped from another connection.
- Manual image publication only moves `:latest` when it publishes the repository's latest release, checked right before promotion.

## [1.3.26] - 2026-11-06

### Fixed
Expand Down
21 changes: 20 additions & 1 deletion rust/plex-pg-core/src/bin/runtime_e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ fn run() -> Result<()> {
&'static str,
fn(&Api, &Db<'_>, &Db<'_>, &mut Client) -> Result<()>,
);
let cases: [Case; 21] = [
let cases: [Case; 22] = [
(
"native_uuid_binding_ownership",
|api, first, _, observer| {
Expand Down Expand Up @@ -1153,6 +1153,19 @@ fn run() -> Result<()> {
)
},
),
(
// `LIMIT 1` reads take the eager (non-streaming) path; a query that
// fails while executing must report an error, not an empty result.
"eager_query_errors",
|api, first, _, _| {
let stmt = first.prepare("SELECT CAST(1 AS INTEGER) / CAST(abs(random()) * 0 AS INTEGER) AS boom LIMIT 1")?;
let code = unsafe { (api.step)(stmt.handle) };
require(code != ROW && code != DONE, "failed eager query reported success")?;
require(!api.error(first.handle).is_empty(), "failed eager query lacks diagnostics")?;
require(unsafe { (api.step)(stmt.handle) } != ROW, "failed eager query produced rows")?;
Ok(())
},
),
(
// Plex 1.43.5 migration 202609141200 records a preference this way;
// a failure aborted the migration on a fresh database.
Expand Down Expand Up @@ -1274,6 +1287,12 @@ fn run() -> Result<()> {
.is_err(),
"interrupted transaction silently resumed in autocommit",
)?;
// The server discarded the transaction with the session; ROLLBACK
// must clear it so the handle can start new transactions.
first.exec("ROLLBACK")?;
first.exec("BEGIN")?;
first.exec("INSERT INTO runtime_items (unique_value) VALUES (603)")?;
first.exec("COMMIT")?;
require(
observer.query_one("SELECT COUNT(*) FROM runtime_e2e.runtime_items WHERE unique_value IN (601,602)", &[]).map_err(|error| error.to_string())?.get::<_, i64>(0) == 0,
"interrupted transaction persisted partial writes",
Expand Down
22 changes: 22 additions & 0 deletions rust/plex-pg-core/src/db_interpose_exec/pg_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ pub(crate) fn exec_via_postgres(
}

let status = crate::libpq_helpers::rust_pq_result_status(res);
// Also on failure: a batch can commit DDL before a later
// statement fails.
note_write_done(pg_conn, CStr::from_ptr(sql).to_bytes(), true);
if status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK {
clear_pg_last_error(pg_conn);
let cmd_tuples = crate::libpq_helpers::rust_pq_cmd_tuples(res);
Expand Down Expand Up @@ -365,3 +368,22 @@ pub(crate) fn exec_via_postgres(
SQLITE_OK
}
}

/// A write ran on `conn`. DDL opens a description-cache window that ends once
/// the session is idle; outside a transaction that is right away.
pub(crate) fn note_write_done(conn: *mut crate::ffi_types::PgConnection, sql: &[u8], batch: bool) {
if conn.is_null() {
return;
}
let ddl = if batch {
crate::descriptor_cache::batch_contains_ddl(sql)
} else {
crate::descriptor_cache::starts_with_ddl(sql)
};
if ddl {
crate::descriptor_cache::ddl_executed(conn as usize);
}
if unsafe { crate::libpq_helpers::rust_pq_transaction_status((*conn).conn) } == 0 {
crate::descriptor_cache::connection_idle(conn as usize);
}
}
14 changes: 14 additions & 0 deletions rust/plex-pg-core/src/db_interpose_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ unsafe fn my_sqlite3_step_impl(p_stmt: *mut sqlite3_stmt) -> c_int {
let original_sql = call_sqlite3_sql(p_stmt);
let sql = crate::db_interpose_helpers::cstr_to_str_or_empty(original_sql);
if !crate::pg_config::is_sqlite_passthrough_str(sql)
&& !statement_is_rollback(p_stmt, sql)
&& !crate::pg_client::transaction::session_ready(handle_conn)
{
return SQLITE_ERROR;
Expand Down Expand Up @@ -472,3 +473,16 @@ unsafe fn my_sqlite3_step_impl(p_stmt: *mut sqlite3_stmt) -> c_int {
}
rc
}

/// ROLLBACK may run on a lost session (it clears the tracked transaction).
/// PostgreSQL statements wrap a placeholder SQLite statement, so the original
/// SQL comes from the shim's statement when there is one.
unsafe fn statement_is_rollback(p_stmt: *mut sqlite3_stmt, sqlite_sql: &str) -> bool {
let pg_stmt = crate::pg_statement::rust_stmt_find(p_stmt as usize) as *mut PgStmt;
let sql = if pg_stmt.is_null() || (*pg_stmt).sql.is_null() {
sqlite_sql
} else {
crate::db_interpose_helpers::cstr_to_str_or_empty((*pg_stmt).sql)
};
crate::pg_client::transaction::is_rollback_sql(sql)
}
3 changes: 3 additions & 0 deletions rust/plex-pg-core/src/db_interpose_step/retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,8 @@ pub(super) fn maybe_retry_step(p_stmt: *mut sqlite3_stmt, rc: c_int) -> Option<c
STEP_RETRY_COUNT.with(|c| c.set(0));
}

// The flag belongs to this step; a stale value would let the next failing
// statement on this thread be retried as a connection error.
STEP_PG_CONN_ERROR.with(|c| c.set(0));
None
}
16 changes: 16 additions & 0 deletions rust/plex-pg-core/src/db_interpose_step_read_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ mod tests {
rust_stmt_free(stmt_ptr);
}

#[test]
fn cross_thread_reexecution_keeps_read_ahead_streams() {
let stmt_ptr = make_stmt();
let s = unsafe { &mut *stmt_ptr };
s.result_conn = 0x1234usize as *mut PgConnection;
s.streaming_mode = 1;
s.spill_state = 1;

// The remaining rows are local; a requery would replay from row 0.
assert!(!should_clear_cross_thread_result(stmt_ptr, 0x5678usize as *mut PgConnection));

let s = unsafe { &mut *stmt_ptr };
s.spill_state = 0;
rust_stmt_free(stmt_ptr);
}

#[test]
fn cross_thread_reexecution_keeps_materialized_eager_results() {
let stmt_ptr = make_stmt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ pub(super) unsafe fn set_pg_conn_error(pg_conn_error_out: *mut c_int) {
}
}

/// `read_done` after the first fetch failed. No row has reached the caller, so
/// when the session was lost the statement stays retryable: the step retry
/// re-runs it on the recovered session (a read prepared from the description
/// cache meets a dead session only here, not at prepare). Any other failure is
/// final.
pub(super) unsafe fn first_fetch_failure_state(pq: *mut crate::libpq_helpers::PGconn) -> c_int {
if crate::libpq_helpers::rust_pq_status(pq) != 0 {
0
} else {
-1
}
}

pub(super) fn first_execute_impl(
pg_stmt: *mut PgStmt,
exec_conn_io: *mut *mut PgConnection,
Expand Down Expand Up @@ -113,6 +126,7 @@ pub(super) fn first_execute_impl(
exec_conn,
&mut stmt_guard,
&mut conn_guard,
pg_conn_error_out,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pub(super) unsafe fn ensure_connection_ready(
log_error("=== END DIAGNOSTIC ===");
log_error("STEP READ: Attempting PQreset...");
crate::libpq_helpers::rust_pq_reset(ec.conn);
crate::descriptor_cache::connection_idle(exec_conn as usize);

if crate::libpq_helpers::rust_pq_status(ec.conn) != CONNECTION_OK {
log_error("STEP READ: PQreset failed, trying fresh PQconnectdb...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(super) unsafe fn eager_fetch_result(
exec_conn: *mut PgConnection,
stmt_guard: &mut Option<StmtGuard>,
conn_guard: &mut PthreadMutexGuard,
pg_conn_error_out: *mut c_int,
) -> c_int {
let s = &mut *pg_stmt;
let ec = &*exec_conn;
Expand Down Expand Up @@ -39,18 +40,34 @@ pub(super) unsafe fn eager_fetch_result(
*stmt_guard = None; // unlock
return STEP_RESULT_ROW;
}
} else if !s.result.is_null() {
let err2 = crate::libpq_helpers::rust_pq_error_message(ec.conn);
let ctx = CString::new("EAGER FALLBACK").unwrap();
let fallback_err = if err2.is_null() {
CString::new("?").unwrap()
} else {
// A failed query is an error, as on the streaming path; it used to
// come back as an empty result with only a log line.
if s.result.is_null() {
crate::db_interpose_exec::pg_path::set_pg_last_error(
s.conn,
"PostgreSQL returned no result",
);
} else {
CString::new(cstr_to_str(err2)).unwrap_or_else(|_| CString::new("?").unwrap())
};
log_sql_fallback(s.sql, s.pg_sql, fallback_err.as_ptr(), ctx.as_ptr());
crate::libpq_helpers::rust_pq_clear(s.result);
s.result = std::ptr::null_mut();
let err2 = crate::libpq_helpers::rust_pq_error_message(ec.conn);
let ctx = CString::new("EAGER FALLBACK").unwrap();
let fallback_err = if err2.is_null() {
CString::new("?").unwrap()
} else {
CString::new(cstr_to_str(err2)).unwrap_or_else(|_| CString::new("?").unwrap())
};
log_sql_fallback(s.sql, s.pg_sql, fallback_err.as_ptr(), ctx.as_ptr());
crate::db_interpose_exec::pg_path::record_pg_result_error(s.conn, exec_conn, s.result);
crate::libpq_helpers::rust_pq_clear(s.result);
s.result = std::ptr::null_mut();
}
s.result_conn = std::ptr::null_mut();
s.read_done = first_fetch_failure_state(ec.conn);
crate::pg_client::rust_pool_check_health(exec_conn as *mut c_void);
*exec_conn_io = exec_conn;
*stmt_guard = None; // unlock
set_pg_conn_error(pg_conn_error_out);
return STEP_RESULT_ERROR;
}

s.read_done = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,3 @@ pub(super) unsafe fn streaming_fetch_result(
set_pg_conn_error(pg_conn_error_out);
STEP_RESULT_ERROR
}

/// `read_done` after the first fetch failed. No row has reached the caller, so
/// when the session was lost the statement stays retryable: the step retry
/// re-runs it on the recovered session (a read prepared from the description
/// cache meets a dead session only here, not at prepare). Any other failure is
/// final.
unsafe fn first_fetch_failure_state(pq: *mut crate::libpq_helpers::PGconn) -> c_int {
if crate::libpq_helpers::rust_pq_status(pq) != 0 {
0
} else {
-1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub(crate) fn should_clear_cross_thread_result(
if stmt.result_conn == exec_conn {
return false;
}
stmt.streaming_mode != 0
// A stream that was read ahead no longer needs its connection: its
// remaining rows are local. Requerying it would deliver from row 0 again.
stmt.streaming_mode != 0 && stmt.spill_state == 0
}

pub(crate) fn should_use_streaming(stmt: *const PgStmt, disable_streaming_env: bool) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub extern "C" fn rust_step_write_prepare_connection(
log_error("=== END DIAGNOSTIC ===");
log_error("STEP WRITE: Attempting PQreset...");
crate::libpq_helpers::rust_pq_reset(ec.conn);
crate::descriptor_cache::connection_idle(exec_conn as usize);
if crate::libpq_helpers::rust_pq_status(ec.conn) != CONNECTION_OK {
log_error("STEP WRITE: PQreset failed, trying fresh PQconnectdb...");
crate::pg_client::rust_stmt_cache_clear(exec_conn as *mut c_void);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub extern "C" fn rust_step_write_execute_and_finalize(
}

crate::db_interpose_exec::pg_path::copy_pg_outcome(handle_conn, exec_conn);
crate::db_interpose_exec::pg_path::note_write_done(exec_conn, cstr_bytes(stmt.sql), false);
stmt.write_executed = 1;
if !res.is_null() {
// A statement Plex wrote with RETURNING produces rows, as in
Expand Down
Loading
Loading