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,242 @@ 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
+
### Simplifying UDF usage without CAST()
239
+
240
+
While CAST() is required for proper function execution, you can configure your MySQL client to handle data type conversions automatically, reducing the need to use CAST() explicitly in your queries.
241
+
242
+
#### Configure the MySQL client
243
+
244
+
You can set the appropriate client character set and collation to simplify UDF usage:
207
245
208
246
```{.bash data-prompt="mysql>"}
209
-
mysql> SELECT ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES \G
247
+
-- Set client character set
248
+
mysql> SET character_set_client = 'utf8mb4';
249
+
250
+
-- Set client collation
251
+
mysql> SET collation_connection = 'utf8mb4_general_ci';
210
252
```
211
253
254
+
Alternatively, you can configure these settings in your MySQL client configuration file (e.g., `~/.my.cnf` or `/etc/mysql/my.cnf`):
255
+
256
+
```ini
257
+
[client]
258
+
default-character-set=utf8mb4
259
+
default-collation=utf8mb4_general_ci
260
+
```
261
+
262
+
By configuring these settings, the MySQL client can handle data type conversions more effectively, allowing you to use the UDF functions without explicit CAST() statements in many cases.
263
+
264
+
!!! note
265
+
266
+
While client configuration can simplify usage, CAST() will still work and may be necessary in some scenarios. The choice between using CAST() explicitly or relying on client configuration depends on your specific use case and preferences.
267
+
268
+
212
269
213
270
### Usage examples
214
271
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.
272
+
The following examples demonstrate how to use each function. Replace the sample arguments with values from your environment. All examples include CAST statements for proper function execution, though these may be optional if you've configured your MySQL client as described in the [Simplifying UDF usage without CAST()](#simplifying-udf-usage-without-cast) section.
217
273
218
-
#### get_binlog_by_gtid()
274
+
#### Find binary log by GTID
219
275
220
-
Locate the binlog that contains a GTID:
276
+
Use `get_binlog_by_gtid()` to locate which binary log file contains a specific transaction:
221
277
222
278
```{.bash data-prompt="mysql>"}
223
-
mysql> SELECT get_binlog_by_gtid('UUID-GROUP:1') AS binlog;
279
+
mysql> SELECT CAST(get_binlog_by_gtid('550e8400-e29b-41d4-a716-446655440000:123') AS CHAR) AS binlog;
224
280
```
225
281
226
-
#### get_last_gtid_from_binlog()
282
+
Use case: When you know a specific GTID and need to find which binary log file contains that transaction for recovery purposes.
283
+
284
+
#### Get last GTID from binary log
227
285
228
-
Return the last GTID in a binlog
286
+
Use `get_last_gtid_from_binlog()` to find the final transaction in a specific binary log file:
229
287
230
288
```{.bash data-prompt="mysql>"}
231
-
mysql> SELECT get_last_gtid_from_binlog('binlog.000001') AS last_gtid;
289
+
mysql> SELECT CAST(get_last_gtid_from_binlog('binlog.000001') AS CHAR) AS last_gtid;
232
290
```
233
291
234
-
#### get_gtid_set_by_binlog()
292
+
Use case: Determine the last transaction processed in a binary log file before rotating to the next file.
235
293
236
-
Return all GTIDs in a binlog
294
+
#### Get all GTIDs from binary log
295
+
296
+
Use `get_gtid_set_by_binlog()` to retrieve all GTIDs contained in a specific binary log file:
237
297
238
298
```{.bash data-prompt="mysql>"}
239
-
mysql> SELECT get_gtid_set_by_binlog('binlog.000001') AS gtid_set;
299
+
mysql> SELECT CAST(get_gtid_set_by_binlog('binlog.000001') AS CHAR) AS gtid_set;
240
300
```
241
301
242
-
#### get_binlog_by_gtid_set()
302
+
Use case: Get a complete list of all transactions in a binary log file for analysis or replication setup.
303
+
304
+
#### Find binary log by GTID set
243
305
244
-
Find a binlog that contains any GTID in a set
306
+
Use `get_binlog_by_gtid_set()` to find the first binary log file that contains any GTID from a specified set:
245
307
246
308
```{.bash data-prompt="mysql>"}
247
-
mysql> SELECT get_binlog_by_gtid_set('UUID1:7,UUID1:8') AS binlog;
309
+
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
310
```
249
311
250
-
#### get_first_record_timestamp_by_binlog() and get_last_record_timestamp_by_binlog(binlog)
312
+
Use case: When you have a set of GTIDs and need to find which binary log file contains at least one of those transactions.
251
313
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.
314
+
#### Get binary log timestamps
254
315
255
-
=== "Raw Timestamp"
316
+
Use timestamp functions to determine when events occurred in binary log files. These functions return microsecond-precision timestamps in UNIX time format.
317
+
318
+
##### Get first event timestamp
319
+
320
+
Find when the first event was written to a binary log file:
321
+
322
+
=== "Raw Timestamp (Microseconds)"
256
323
```{.bash data-prompt="mysql>"}
257
-
mysql> SELECT get_first_record_timestamp_by_binlog('binlog.000001') AS raw_ts;
324
+
mysql> SELECT CAST(get_first_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) AS raw_ts;
258
325
```
259
326
260
-
=== "Human-Readable"
327
+
=== "Human-Readable Format"
261
328
```{.bash data-prompt="mysql>"}
262
329
mysql> SELECT FROM_UNIXTIME(
263
-
get_first_record_timestamp_by_binlog('binlog.000001') DIV 1000000
330
+
CAST(get_first_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) DIV 1000000
264
331
) AS first_event_ts;
265
332
```
266
333
267
-
Get the last event timestamp from a binlog
334
+
Use case: Determine when a binary log file started receiving events, useful for recovery planning.
335
+
336
+
##### Get last event timestamp
268
337
269
-
=== "Raw Timestamp"
338
+
Find when the last event was written to a binary log file:
339
+
340
+
=== "Raw Timestamp (Microseconds)"
270
341
```{.bash data-prompt="mysql>"}
271
-
mysql> SELECT get_last_record_timestamp_by_binlog('binlog.000001') AS raw_ts;
342
+
mysql> SELECT CAST(get_last_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) AS raw_ts;
272
343
```
273
344
274
-
=== "Human-Readable"
345
+
=== "Human-Readable Format"
275
346
```{.bash data-prompt="mysql>"}
276
347
mysql> SELECT FROM_UNIXTIME(
277
-
get_last_record_timestamp_by_binlog('binlog.000001') DIV 1000000
348
+
CAST(get_last_record_timestamp_by_binlog('binlog.000001') AS UNSIGNED) DIV 1000000
278
349
) AS last_event_ts;
279
350
```
280
351
352
+
Use case: Determine when a binary log file stopped receiving events, useful for understanding binary log rotation timing.
353
+
354
+
355
+
### Troubleshooting
356
+
357
+
#### Common issues
358
+
359
+
Function returns NULL: This usually indicates that the specified GTID or binary log file does not exist. Verify that:
360
+
361
+
* The GTID format is correct (UUID:transaction_id)
362
+
363
+
* The binary log file exists in the binary log directory
364
+
365
+
* GTID is enabled on the server
281
366
282
-
??? example "Expected output"
367
+
Error: "Unknown function": The component is not installed. Install the component using the `INSTALL COMPONENT` command.
283
368
284
-
```{.text .no-copy}
285
-
+---------------+
286
-
| binlog |
287
-
+---------------+
288
-
| binlog.000001 |
289
-
+---------------+
290
-
```
369
+
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+.
291
370
292
-
Actual values depend on your server state and binlog contents.
371
+
Performance issues: These functions read binary log files directly from disk. For large binary log files, expect execution times of several seconds.
372
+
373
+
#### Verify binary log files
374
+
375
+
Check which binary log files are available:
376
+
377
+
```{.bash data-prompt="mysql>"}
378
+
mysql> SHOW BINARY LOGS;
379
+
```
380
+
381
+
#### Check GTID status
382
+
383
+
Verify GTID is enabled:
384
+
385
+
```{.bash data-prompt="mysql>"}
386
+
mysql> SHOW VARIABLES LIKE 'gtid_mode';
387
+
```
293
388
294
389
### Uninstall the component
295
390
296
-
Remove the component and all associated UDFs:
391
+
Remove the component and all associated functions:
0 commit comments