@@ -32,7 +32,6 @@ import (
32
32
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descbuilder"
33
33
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
34
34
"github.com/cockroachdb/cockroach/pkg/sql/catalog/internal/catkv"
35
- "github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
36
35
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
37
36
"github.com/cockroachdb/cockroach/pkg/sql/enum"
38
37
"github.com/cockroachdb/cockroach/pkg/sql/isql"
@@ -1346,7 +1345,7 @@ type Manager struct {
1346
1345
settings * cluster.Settings
1347
1346
mu struct {
1348
1347
syncutil.Mutex
1349
- // TODO(james): Track size of leased descriptors in memory.
1348
+
1350
1349
descriptors map [descpb.ID ]* descriptorState
1351
1350
1352
1351
// Session based leases that will be removed with expiry, since
@@ -2732,27 +2731,36 @@ func (m *Manager) deleteOrphanedLeasesFromStaleSession(
2732
2731
2733
2732
var distinctSessions []tree.Datums
2734
2733
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 ()}
2737
2734
const limit = 50
2738
2735
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
+
2739
2750
totalSessionsProcessed := 0
2740
2751
totalLeasesDeleted := 0
2741
2752
2742
2753
for r := retry .StartWithCtx (ctx , retryOpts ); r .Next (); {
2743
2754
// Get a list of distinct, dead session IDs that exist in the system.lease
2744
2755
// 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
+ )
2756
2764
if err != nil {
2757
2765
if ! startup .IsRetryableReplicaError (err ) {
2758
2766
log .Dev .Warningf (ctx , "unable to read session IDs for orphaned leases: %v" , err )
@@ -2767,7 +2775,7 @@ func (m *Manager) deleteOrphanedLeasesFromStaleSession(
2767
2775
// Delete rows in our lease table with orphaned sessions.
2768
2776
for _ , sessionRow := range distinctSessions {
2769
2777
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 )
2771
2779
if err != nil {
2772
2780
log .Dev .Warningf (ctx , "unable to delete orphaned leases for session %s: %v" , sessionID , err )
2773
2781
break
@@ -2802,25 +2810,32 @@ func deleteLeaseWithSessionIDWithBatch(
2802
2810
ctx context.Context ,
2803
2811
ex isql.Executor ,
2804
2812
retryOpts retry.Options ,
2805
- syntheticDescriptors catalog. Descriptors ,
2813
+ multiRegionSystemDb bool ,
2806
2814
sessionID sqlliveness.SessionID ,
2807
- region string ,
2815
+ regionParam interface {} ,
2808
2816
batchSize int ,
2809
2817
) (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
+
2810
2828
totalDeleted := 0
2811
2829
for r := retry .StartWithCtx (ctx , retryOpts ); r .Next (); {
2812
2830
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 {
2824
2839
if ! startup .IsRetryableReplicaError (err ) {
2825
2840
return totalDeleted , err
2826
2841
}
0 commit comments