You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -165,139 +165,212 @@ mysql> SET binlog_ddl_skip_rewrite = ON;
165
165
166
166
## Point-in-Time Recovery with `binlog_utils_udf`
167
167
168
-
Use the binlog_utils_udf component to assist with Point-in-Time Recovery (PiTR).
169
-
The component installs user-defined functions (UDFs) that help you map GTIDs to
170
-
binary log files and inspect the contents and timestamps of binlog files.
168
+
Point-in-Time Recovery (PiTR) allows you to restore a database to any specific moment in time using binary logs. The `binlog_utils_udf` component provides user-defined functions (UDFs) that simplify PiTR operations by helping you:
171
169
172
-
### Functions
170
+
* Map Global Transaction Identifiers (GTIDs) to specific binary log files
171
+
* Inspect binary log contents and timestamps
172
+
* Locate the exact binary log files needed for recovery operations
| get_binlog_by_gtid(gtid) | STRING (binlog name) | Returns the binlog file that contains the specified GTID. |
177
-
| get_last_gtid_from_binlog(binlog) | STRING (GTID) | Returns the last GTID found in the specified binlog. |
178
-
| get_gtid_set_by_binlog(binlog) | STRING (GTID set) | Returns all GTIDs found in the specified binlog. |
179
-
| get_binlog_by_gtid_set(gtid_set) | STRING (binlog name) | Returns the first binlog file that contains at least one GTID from the specified set. |
180
-
| get_first_record_timestamp_by_binlog(binlog) | INTEGER (timestamp) | Returns the timestamp of the first event in the specified binlog. |
181
-
| get_last_record_timestamp_by_binlog(binlog) | INTEGER (timestamp) | Returns the timestamp of the last event in the specified binlog. |
174
+
These functions are particularly useful when you need to determine which binary log files contain specific transactions or events during recovery planning.
182
175
183
-
### Notes
176
+
### Prerequisites
184
177
185
-
* Timestamp-returning functions provide values with microsecond precision in
186
-
UNIX time. Each value represents the number of microseconds since
187
-
1970-01-01 00:00:00 UTC.
178
+
Before using the `binlog_utils_udf` component, ensure the following requirements are met:
188
179
189
-
* Functions that accept a binlog name require a short file name only. Do not
190
-
include a path. If the input contains a path separator (/), the server
191
-
returns an error.
180
+
* Percona Server for MySQL: The component is only available in Percona Server for MySQL, not in standard MySQL
192
181
193
-
* The server reads binlogs from the current binlog directory defined by the
194
-
@@log_bin_basename system variable.
182
+
* Binary logging enabled: The server must have binary logging enabled (`log_bin` system variable set to `ON`)
195
183
196
-
*Functions that return a binlog file name return the short name (no path).
184
+
*GTID enabled: For GTID-related functions, GTID must be enabled (`gtid_mode` set to `ON`)
197
185
198
-
### Install the component
186
+
* MySQL privileges: You need `SYSTEM_VARIABLES_ADMIN` privilege to install components. For binary log operations, `BINLOG_ADMIN` privilege may also be required. The `SUPER` privilege is deprecated in MySQL 8.0+ and should be replaced with specific dynamic privileges
199
187
200
-
Install the component once on each server where you want to use these UDFs.
188
+
#### Install the component
189
+
190
+
Install the component on each server where you plan to use these functions:
|[`get_binlog_by_gtid(gtid)`](#find-binary-log-by-gtid)| STRING (binlog name) | Returns the binary log file that contains the specified GTID. | Find which binary log contains a specific transaction |
218
+
|[`get_last_gtid_from_binlog(binlog)`](#get-last-gtid-from-binary-log)| STRING (GTID) | Returns the last GTID found in the specified binary log. | Identify the final transaction in a binary log file |
219
+
|[`get_gtid_set_by_binlog(binlog)`](#get-all-gtids-from-binary-log)| STRING (GTID set) | Returns all GTIDs found in the specified binary log. | Get complete list of transactions in a binary log |
220
+
|[`get_binlog_by_gtid_set(gtid_set)`](#find-binary-log-by-gtid-set)| STRING (binlog name) | Returns the first binary log file that contains at least one GTID from the specified set. | Find binary log containing any transaction from a GTID set |
221
+
|[`get_first_record_timestamp_by_binlog(binlog)`](#get-first-event-timestamp)| INTEGER (timestamp) | Returns the timestamp of the first event in the specified binary log. | Determine when a binary log file started |
222
+
|[`get_last_record_timestamp_by_binlog(binlog)`](#get-last-event-timestamp)| INTEGER (timestamp) | Returns the timestamp of the last event in the specified binary log. | Determine when a binary log file ended |
223
+
224
+
### Important notes
225
+
226
+
* CAST requirement: When using these user-defined functions, you must use CAST to return a result. String functions require `CAST(...AS CHAR)` and timestamp functions require `CAST(...AS UNSIGNED)`.
227
+
228
+
* Timestamp precision: Timestamp-returning functions provide values with microsecond precision in UNIX time format. Each value represents the number of microseconds since 1970-01-01 00:00:00 UTC.
229
+
230
+
* Binary log file names: Functions that accept a binary log name require only the short file name (for example, `binlog.000001`). Do not include the full path. If the input contains a path separator (`/`), the server returns an error.
231
+
232
+
* Binary log directory: The server reads binary logs from the directory defined by the `@@log_bin_basename` system variable.
233
+
234
+
* Return values: Functions that return binary log file names return only the short name without the path.
235
+
236
+
* Performance considerations: These functions read binary log files directly from disk. For large binary log files, the functions may take several seconds to complete.
237
+
238
+
212
239
213
240
### Usage examples
214
241
215
-
Replace the sample arguments with values from your environment. The examples
216
-
show the typical way to call each function. For clarity, results are aliased.
242
+
The following examples demonstrate how to use each function. Replace the sample arguments with values from your environment. All examples include the required CAST statements for proper function execution.
217
243
218
-
#### get_binlog_by_gtid()
244
+
#### Find binary log by GTID
219
245
220
-
Locate the binlog that contains a GTID:
246
+
Use `get_binlog_by_gtid()` to locate which binary log file contains a specific transaction:
221
247
222
248
```{.bash data-prompt="mysql>"}
223
-
mysql> SELECT get_binlog_by_gtid('UUID-GROUP:1') AS binlog;
249
+
mysql> SELECT CAST(get_binlog_by_gtid('550e8400-e29b-41d4-a716-446655440000:123') AS CHAR) AS binlog;
224
250
```
225
251
226
-
#### get_last_gtid_from_binlog()
252
+
Use case: When you know a specific GTID and need to find which binary log file contains that transaction for recovery purposes.
253
+
254
+
#### Get last GTID from binary log
227
255
228
-
Return the last GTID in a binlog
256
+
Use `get_last_gtid_from_binlog()` to find the final transaction in a specific binary log file:
229
257
230
258
```{.bash data-prompt="mysql>"}
231
-
mysql> SELECT get_last_gtid_from_binlog('binlog.000001') AS last_gtid;
259
+
mysql> SELECT CAST(get_last_gtid_from_binlog('binlog.000001') AS CHAR) AS last_gtid;
232
260
```
233
261
234
-
#### get_gtid_set_by_binlog()
262
+
Use case: Determine the last transaction processed in a binary log file before rotating to the next file.
235
263
236
-
Return all GTIDs in a binlog
264
+
#### Get all GTIDs from binary log
265
+
266
+
Use `get_gtid_set_by_binlog()` to retrieve all GTIDs contained in a specific binary log file:
237
267
238
268
```{.bash data-prompt="mysql>"}
239
-
mysql> SELECT get_gtid_set_by_binlog('binlog.000001') AS gtid_set;
269
+
mysql> SELECT CAST(get_gtid_set_by_binlog('binlog.000001') AS CHAR) AS gtid_set;
240
270
```
241
271
242
-
#### get_binlog_by_gtid_set()
272
+
Use case: Get a complete list of all transactions in a binary log file for analysis or replication setup.
273
+
274
+
#### Find binary log by GTID set
243
275
244
-
Find a binlog that contains any GTID in a set
276
+
Use `get_binlog_by_gtid_set()` to find the first binary log file that contains any GTID from a specified set:
245
277
246
278
```{.bash data-prompt="mysql>"}
247
-
mysql> SELECT get_binlog_by_gtid_set('UUID1:7,UUID1:8') AS binlog;
279
+
mysql> SELECT CAST(get_binlog_by_gtid_set('550e8400-e29b-41d4-a716-446655440000:7,550e8400-e29b-41d4-a716-446655440000:8') AS CHAR) AS binlog;
248
280
```
249
281
250
-
#### get_first_record_timestamp_by_binlog() and get_last_record_timestamp_by_binlog(binlog)
282
+
Use case: When you have a set of GTIDs and need to find which binary log file contains at least one of those transactions.
283
+
284
+
#### Get binary log timestamps
251
285
252
-
Get the first event timestamp from a binlog. The function returns microseconds since the UNIX epoch. Use the tabs below to
253
-
see the raw numeric value or a human-readable timestamp.
286
+
Use timestamp functions to determine when events occurred in binary log files. These functions return microsecond-precision timestamps in UNIX time format.
254
287
255
-
=== "Raw Timestamp"
288
+
##### Get first event timestamp
289
+
290
+
Find when the first event was written to a binary log file:
291
+
292
+
=== "Raw Timestamp (Microseconds)"
256
293
```{.bash data-prompt="mysql>"}
257
-
mysql> SELECT get_first_record_timestamp_by_binlog('binlog.000001') AS raw_ts;
294
+
mysql> SELECT CAST(get_first_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) AS raw_ts;
258
295
```
259
296
260
-
=== "Human-Readable"
297
+
=== "Human-Readable Format"
261
298
```{.bash data-prompt="mysql>"}
262
299
mysql> SELECT FROM_UNIXTIME(
263
-
get_first_record_timestamp_by_binlog('binlog.000001') DIV 1000000
300
+
CAST(get_first_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) DIV 1000000
264
301
) AS first_event_ts;
265
302
```
266
303
267
-
Get the last event timestamp from a binlog
304
+
Use case: Determine when a binary log file started receiving events, useful for recovery planning.
305
+
306
+
##### Get last event timestamp
268
307
269
-
=== "Raw Timestamp"
308
+
Find when the last event was written to a binary log file:
309
+
310
+
=== "Raw Timestamp (Microseconds)"
270
311
```{.bash data-prompt="mysql>"}
271
-
mysql> SELECT get_last_record_timestamp_by_binlog('binlog.000001') AS raw_ts;
312
+
mysql> SELECT CAST(get_last_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) AS raw_ts;
272
313
```
273
314
274
-
=== "Human-Readable"
315
+
=== "Human-Readable Format"
275
316
```{.bash data-prompt="mysql>"}
276
317
mysql> SELECT FROM_UNIXTIME(
277
-
get_last_record_timestamp_by_binlog('binlog.000001') DIV 1000000
318
+
CAST(get_last_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) DIV 1000000
278
319
) AS last_event_ts;
279
320
```
280
321
322
+
Use case: Determine when a binary log file stopped receiving events, useful for understanding binary log rotation timing.
323
+
324
+
325
+
### Troubleshooting
326
+
327
+
#### Common issues
328
+
329
+
Function returns NULL: This usually indicates that the specified GTID or binary log file does not exist. Verify that:
330
+
331
+
* The GTID format is correct (UUID:transaction_id)
281
332
282
-
??? example "Expected output"
333
+
* The binary log file exists in the binary log directory
283
334
284
-
```{.text .no-copy}
285
-
+---------------+
286
-
| binlog |
287
-
+---------------+
288
-
| binlog.000001 |
289
-
+---------------+
290
-
```
335
+
* GTID is enabled on the server
291
336
292
-
Actual values depend on your server state and binlog contents.
337
+
Error: "Unknown function": The component is not installed. Install the component using the `INSTALL COMPONENT` command.
338
+
339
+
Error: "Access denied": You need `SYSTEM_VARIABLES_ADMIN` privilege to install the component and `BINLOG_ADMIN` privilege for binary log operations. The `SUPER` privilege is deprecated in MySQL 8.0+.
340
+
341
+
Performance issues: These functions read binary log files directly from disk. For large binary log files, expect execution times of several seconds.
342
+
343
+
#### Verify binary log files
344
+
345
+
Check which binary log files are available:
346
+
347
+
```{.bash data-prompt="mysql>"}
348
+
mysql> SHOW BINARY LOGS;
349
+
```
350
+
351
+
#### Check GTID status
352
+
353
+
Verify GTID is enabled:
354
+
355
+
```{.bash data-prompt="mysql>"}
356
+
mysql> SHOW VARIABLES LIKE 'gtid_mode';
357
+
```
293
358
294
359
### Uninstall the component
295
360
296
-
Remove the component and all associated UDFs:
361
+
Remove the component and all associated functions:
0 commit comments