@@ -32,7 +32,6 @@ import (
3232 "github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder"
3333 "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
3434 "github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/catkv"
35- "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
3635 "github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
3736 "github.com/cockroachdb/cockroach/pkg/sql/enum"
3837 "github.com/cockroachdb/cockroach/pkg/sql/isql"
@@ -1346,7 +1345,7 @@ type Manager struct {
13461345 settings * cluster.Settings
13471346 mu struct {
13481347 syncutil.Mutex
1349- // TODO(james): Track size of leased descriptors in memory.
1348+
13501349 descriptors map [descpb.ID ]* descriptorState
13511350
13521351 // Session based leases that will be removed with expiry, since
@@ -2732,27 +2731,36 @@ func (m *Manager) deleteOrphanedLeasesFromStaleSession(
27322731
27332732 var distinctSessions []tree.Datums
27342733 aostTime := hlc.Timestamp {WallTime : initialTimestamp }
2735- distinctSessionQuery := `SELECT DISTINCT(session_id) FROM system.lease AS OF SYSTEM TIME %s WHERE crdb_region=$1 AND NOT crdb_internal.sql_liveness_is_alive(session_id, true) LIMIT $2`
2736- syntheticDescriptors := catalog.Descriptors {systemschema .LeaseTable ()}
27372734 const limit = 50
27382735
2736+ // Build the query based on whether the system database is multi-region.
2737+ // For multi-region, we need to cast the region parameter to the enum type.
2738+ // For single-region, we use the bytes representation.
2739+ var distinctSessionQuery string
2740+ var regionParam interface {}
2741+ isMultiRegion := bool (multiRegionSystemDb )
2742+ if isMultiRegion {
2743+ distinctSessionQuery = `SELECT DISTINCT(session_id) FROM system.lease AS OF SYSTEM TIME %s WHERE crdb_region=$1::system.crdb_internal_region AND NOT crdb_internal.sql_liveness_is_alive(session_id, true) LIMIT $2`
2744+ regionParam = region
2745+ } else {
2746+ distinctSessionQuery = `SELECT DISTINCT(session_id) FROM system.lease AS OF SYSTEM TIME %s WHERE crdb_region=$1 AND NOT crdb_internal.sql_liveness_is_alive(session_id, true) LIMIT $2`
2747+ regionParam = enum .One
2748+ }
2749+
27392750 totalSessionsProcessed := 0
27402751 totalLeasesDeleted := 0
27412752
27422753 for r := retry .StartWithCtx (ctx , retryOpts ); r .Next (); {
27432754 // Get a list of distinct, dead session IDs that exist in the system.lease
27442755 // table.
2745- err = ex .WithSyntheticDescriptors (syntheticDescriptors , func () error {
2746- distinctSessions , err = ex .QueryBufferedEx (ctx ,
2747- "query-lease-table-dead-sessions" ,
2748- nil ,
2749- sessiondata .NodeUserSessionDataOverride ,
2750- fmt .Sprintf (distinctSessionQuery , aostTime .AsOfSystemTime ()),
2751- region ,
2752- limit ,
2753- )
2754- return err
2755- })
2756+ distinctSessions , err = ex .QueryBufferedEx (ctx ,
2757+ "query-lease-table-dead-sessions" ,
2758+ nil ,
2759+ sessiondata .NodeUserSessionDataOverride ,
2760+ fmt .Sprintf (distinctSessionQuery , aostTime .AsOfSystemTime ()),
2761+ regionParam ,
2762+ limit ,
2763+ )
27562764 if err != nil {
27572765 if ! startup .IsRetryableReplicaError (err ) {
27582766 log .Dev .Warningf (ctx , "unable to read session IDs for orphaned leases: %v" , err )
@@ -2767,7 +2775,7 @@ func (m *Manager) deleteOrphanedLeasesFromStaleSession(
27672775 // Delete rows in our lease table with orphaned sessions.
27682776 for _ , sessionRow := range distinctSessions {
27692777 sessionID := sqlliveness .SessionID (tree .MustBeDBytes (sessionRow [0 ]))
2770- sessionLeasesDeleted , err := deleteLeaseWithSessionIDWithBatch (ctx , ex , retryOpts , syntheticDescriptors , sessionID , region , limit )
2778+ sessionLeasesDeleted , err := deleteLeaseWithSessionIDWithBatch (ctx , ex , retryOpts , isMultiRegion , sessionID , regionParam , limit )
27712779 if err != nil {
27722780 log .Dev .Warningf (ctx , "unable to delete orphaned leases for session %s: %v" , sessionID , err )
27732781 break
@@ -2802,25 +2810,32 @@ func deleteLeaseWithSessionIDWithBatch(
28022810 ctx context.Context ,
28032811 ex isql.Executor ,
28042812 retryOpts retry.Options ,
2805- syntheticDescriptors catalog. Descriptors ,
2813+ multiRegionSystemDb bool ,
28062814 sessionID sqlliveness.SessionID ,
2807- region string ,
2815+ regionParam interface {} ,
28082816 batchSize int ,
28092817) (int , error ) {
2818+ // Build the query based on whether the system database is multi-region.
2819+ // For multi-region, we need to cast the region parameter to the enum type.
2820+ // For single-region, we use the bytes representation.
2821+ var deleteOrphanedQuery string
2822+ if multiRegionSystemDb {
2823+ deleteOrphanedQuery = `DELETE FROM system.lease WHERE session_id=$1 AND crdb_region=$2::system.crdb_internal_region LIMIT $3`
2824+ } else {
2825+ deleteOrphanedQuery = `DELETE FROM system.lease WHERE session_id=$1 AND crdb_region=$2 LIMIT $3`
2826+ }
2827+
28102828 totalDeleted := 0
28112829 for r := retry .StartWithCtx (ctx , retryOpts ); r .Next (); {
28122830 var rowsDeleted int
2813- deleteOrphanedQuery := `DELETE FROM system.lease WHERE session_id=$1 AND crdb_region=$2 LIMIT $3`
2814- if err := ex .WithSyntheticDescriptors (syntheticDescriptors , func () error {
2815- var err error
2816- rowsDeleted , err = ex .ExecEx (ctx , "delete-orphaned-leases-by-session" , nil ,
2817- sessiondata .NodeUserSessionDataOverride ,
2818- deleteOrphanedQuery ,
2819- sessionID .UnsafeBytes (),
2820- region ,
2821- batchSize )
2822- return err
2823- }); err != nil {
2831+ var err error
2832+ rowsDeleted , err = ex .ExecEx (ctx , "delete-orphaned-leases-by-session" , nil ,
2833+ sessiondata .NodeUserSessionDataOverride ,
2834+ deleteOrphanedQuery ,
2835+ sessionID .UnsafeBytes (),
2836+ regionParam ,
2837+ batchSize )
2838+ if err != nil {
28242839 if ! startup .IsRetryableReplicaError (err ) {
28252840 return totalDeleted , err
28262841 }
0 commit comments