cockroachdb / cockroachdb/cockroach
sql: not able to use follower_read_timestamp in multiple AOST statements in AOST txn
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
When using AOST, it must be the same time for every statement in the transaction. CRDB allows AOST on individual statements as long as they match the transaction AOST:
```sql
CREATE TABLE a (a INT);
CREATE TABLE b (b INT);
BEGIN;
SET TRANSACTION AS OF SYSTEM TIME '2025-01-31 00:00:00';
SELECT * FROM a AS OF SYSTEM TIME '2025-01-31 00:00:00';
SELECT * FROM b AS OF SYSTEM TIME '2025-01-31 00:00:00';
COMMIT;
```
But this doesn't work with `follower_read_timestamp`, because it's a volatile function, and returns a different value for each call:
```sql
BEGIN;
SET TRANSACTION AS OF SYSTEM TIME follower_read_timestamp();
SELECT * FROM a AS OF SYSTEM TIME follower_read_timestamp();
-- fails with "inconsistent AS OF SYSTEM TIME timestamp" error
ROLLBACK;
```
One workaround is to read the value of follower_read_timestamp first and then use that absolute value:
```sql
BEGIN;
SELECT follower_read_timestamp();
-- this returns 2025-01-31 19:40:35.875332+00
SET TRANSACTION AS OF SYSTEM TIME '2025-01-31 19:40:35.875332+00';
SELECT * FROM a AS OF SYSTEM TIME '2025-01-31 19:40:35.875332+00';
SELECT * FROM b AS OF SYSTEM TIME '2025-01-31 19:40:35.875332+00';
COMMIT;
```
Jira issue: CRDB-47074
Contributor guide
Assessment
This issue has not been assessed yet.