Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86f3169

Browse files
committed
Add txid_current_ifassigned().
Add a variant of txid_current() that returns NULL if no transaction IDis assigned. This version can be used even on a standby server,although it will always return NULL since no transaction IDs can beassigned during recovery.Craig Ringer, per suggestion from Jim Nasby. Reviewed by Petr Jelinekand by me.
1 parentff36700 commit86f3169

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17119,6 +17119,10 @@ SELECT collation for ('foo' COLLATE "de_DE");
1711917119
<primary>txid_current</primary>
1712017120
</indexterm>
1712117121

17122+
<indexterm>
17123+
<primary>txid_current_if_assigned</primary>
17124+
</indexterm>
17125+
1712217126
<indexterm>
1712317127
<primary>txid_current_snapshot</primary>
1712417128
</indexterm>
@@ -17159,6 +17163,11 @@ SELECT collation for ('foo' COLLATE "de_DE");
1715917163
<entry><type>bigint</type></entry>
1716017164
<entry>get current transaction ID, assigning a new one if the current transaction does not have one</entry>
1716117165
</row>
17166+
<row>
17167+
<entry><literal><function>txid_current_if_assigned()</function></literal></entry>
17168+
<entry><type>bigint</type></entry>
17169+
<entry>same as <function>txid_current()</function> but returns null instead of assigning an xid if none is already assigned</entry>
17170+
</row>
1716217171
<row>
1716317172
<entry><literal><function>txid_current_snapshot()</function></literal></entry>
1716417173
<entry><type>txid_snapshot</type></entry>

‎src/backend/utils/adt/txid.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,27 @@ txid_current(PG_FUNCTION_ARGS)
376376
PG_RETURN_INT64(val);
377377
}
378378

379+
/*
380+
* Same as txid_current() but doesn't assign a new xid if there isn't one
381+
* yet.
382+
*/
383+
Datum
384+
txid_current_if_assigned(PG_FUNCTION_ARGS)
385+
{
386+
txidval;
387+
TxidEpochstate;
388+
TransactionIdtopxid=GetTopTransactionIdIfAny();
389+
390+
if (topxid==InvalidTransactionId)
391+
PG_RETURN_NULL();
392+
393+
load_xid_epoch(&state);
394+
395+
val=convert_xid(topxid,&state);
396+
397+
PG_RETURN_INT64(val);
398+
}
399+
379400
/*
380401
* txid_current_snapshot() returns txid_snapshot
381402
*

‎src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,6 +4904,8 @@ DATA(insert OID = 2942 ( txid_snapshot_sendPGNSP PGUID 12 1 0 0 0 f f f f t
49044904
DESCR("I/O");
49054905
DATA(insert OID = 2943 ( txid_currentPGNSP PGUID 12 1 0 0 0 f f f f t f s u 0 0 20 "" _null_ _null_ _null_ _null_ _null_ txid_current _null_ _null_ _null_ ));
49064906
DESCR("get current transaction ID");
4907+
DATA(insert OID = 3348 ( txid_current_if_assignedPGNSP PGUID 12 1 0 0 0 f f f f t f s u 0 0 20 "" _null_ _null_ _null_ _null_ _null_ txid_current_if_assigned _null_ _null_ _null_ ));
4908+
DESCR("get current transaction ID");
49074909
DATA(insert OID = 2944 ( txid_current_snapshotPGNSP PGUID 12 1 0 0 0 f f f f t f s s 0 0 2970 "" _null_ _null_ _null_ _null_ _null_ txid_current_snapshot _null_ _null_ _null_ ));
49084910
DESCR("get current snapshot");
49094911
DATA(insert OID = 2945 ( txid_snapshot_xminPGNSP PGUID 12 1 0 0 0 f f f f t f i s 1 0 20 "2970" _null_ _null_ _null_ _null_ _null_ txid_snapshot_xmin _null_ _null_ _null_ ));

‎src/include/utils/builtins.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,7 @@ extern Datum txid_snapshot_out(PG_FUNCTION_ARGS);
12211221
externDatumtxid_snapshot_recv(PG_FUNCTION_ARGS);
12221222
externDatumtxid_snapshot_send(PG_FUNCTION_ARGS);
12231223
externDatumtxid_current(PG_FUNCTION_ARGS);
1224+
externDatumtxid_current_if_assigned(PG_FUNCTION_ARGS);
12241225
externDatumtxid_current_snapshot(PG_FUNCTION_ARGS);
12251226
externDatumtxid_snapshot_xmin(PG_FUNCTION_ARGS);
12261227
externDatumtxid_snapshot_xmax(PG_FUNCTION_ARGS);

‎src/test/regress/expected/txid.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,19 @@ SELECT txid_snapshot '1:9223372036854775808:3';
238238
ERROR: invalid input syntax for type txid_snapshot: "1:9223372036854775808:3"
239239
LINE 1: SELECT txid_snapshot '1:9223372036854775808:3';
240240
^
241+
-- test txid_current_if_assigned
242+
BEGIN;
243+
SELECT txid_current_if_assigned() IS NULL;
244+
?column?
245+
----------
246+
t
247+
(1 row)
248+
249+
SELECT txid_current() \gset
250+
SELECT txid_current_if_assigned() IS NOT DISTINCT FROM BIGINT :'txid_current';
251+
?column?
252+
----------
253+
t
254+
(1 row)
255+
256+
COMMIT;

‎src/test/regress/sql/txid.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ select txid_visible_in_snapshot('1000100010001015', '1000100010001000:1000100010
5252
-- test 64bit overflow
5353
SELECT txid_snapshot'1:9223372036854775807:3';
5454
SELECT txid_snapshot'1:9223372036854775808:3';
55+
56+
-- test txid_current_if_assigned
57+
BEGIN;
58+
SELECT txid_current_if_assigned() ISNULL;
59+
SELECT txid_current() \gset
60+
SELECT txid_current_if_assigned() IS NOT DISTINCTFROMBIGINT :'txid_current';
61+
COMMIT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp