Commit 7b94e999 authored by Michael Paquier's avatar Michael Paquier

Remove catalog function currtid()

currtid() and currtid2() are an undocumented set of functions whose sole
known user is the Postgres ODBC driver, able to retrieve the latest TID
version for a tuple given by the caller of those functions.

As used by Postgres ODBC, currtid() is a shortcut able to retrieve the
last TID loaded into a backend by passing an OID of 0 (magic value)
after a tuple insertion.  This is removed in this commit, as it became
obsolete after the driver began using "RETURNING ctid" with inserts, a
clause supported since Postgres 8.2 (using RETURNING is better for
performance anyway as it reduces the number of round-trips to the
backend).

currtid2() is still used by the driver, so this remains around for now.
Note that this function is kept in its original shape for backward
compatibility reasons.

Per discussion with many people, including Andres Freund, Peter
Eisentraut, Álvaro Herrera, Hiroshi Inoue, Tom Lane and myself.

Bump catalog version.

Discussion: https://postgr.es/m/20200603021448.GB89559@paquier.xyz
parent 660b8992
...@@ -645,10 +645,7 @@ ExecInsert(ModifyTableState *mtstate, ...@@ -645,10 +645,7 @@ ExecInsert(ModifyTableState *mtstate,
} }
if (canSetTag) if (canSetTag)
{
(estate->es_processed)++; (estate->es_processed)++;
setLastTid(&slot->tts_tid);
}
/* /*
* If this insert is the result of a partition key update that moved the * If this insert is the result of a partition key update that moved the
......
...@@ -47,6 +47,8 @@ ...@@ -47,6 +47,8 @@
#define DELIM ',' #define DELIM ','
#define NTIDARGS 2 #define NTIDARGS 2
static ItemPointer currtid_for_view(Relation viewrel, ItemPointer tid);
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* tidin * tidin
* ---------------------------------------------------------------- * ----------------------------------------------------------------
...@@ -275,12 +277,44 @@ hashtidextended(PG_FUNCTION_ARGS) ...@@ -275,12 +277,44 @@ hashtidextended(PG_FUNCTION_ARGS)
* Maybe these implementations should be moved to another place * Maybe these implementations should be moved to another place
*/ */
static ItemPointerData Current_last_tid = {{0, 0}, 0}; /*
* Utility wrapper for current CTID functions.
void * Returns the latest version of a tuple pointing at "tid" for
setLastTid(const ItemPointer tid) * relation "rel".
*/
static ItemPointer
currtid_internal(Relation rel, ItemPointer tid)
{ {
Current_last_tid = *tid; ItemPointer result;
AclResult aclresult;
Snapshot snapshot;
TableScanDesc scan;
result = (ItemPointer) palloc(sizeof(ItemPointerData));
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
ACL_SELECT);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
RelationGetRelationName(rel));
if (rel->rd_rel->relkind == RELKIND_VIEW)
return currtid_for_view(rel, tid);
if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel));
ItemPointerCopy(tid, result);
snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = table_beginscan_tid(rel, snapshot);
table_tuple_get_latest_tid(scan, result);
table_endscan(scan);
UnregisterSnapshot(snapshot);
return result;
} }
/* /*
...@@ -288,7 +322,7 @@ setLastTid(const ItemPointer tid) ...@@ -288,7 +322,7 @@ setLastTid(const ItemPointer tid)
* CTID should be defined in the view and it must * CTID should be defined in the view and it must
* correspond to the CTID of a base relation. * correspond to the CTID of a base relation.
*/ */
static Datum static ItemPointer
currtid_for_view(Relation viewrel, ItemPointer tid) currtid_for_view(Relation viewrel, ItemPointer tid)
{ {
TupleDesc att = RelationGetDescr(viewrel); TupleDesc att = RelationGetDescr(viewrel);
...@@ -338,12 +372,12 @@ currtid_for_view(Relation viewrel, ItemPointer tid) ...@@ -338,12 +372,12 @@ currtid_for_view(Relation viewrel, ItemPointer tid)
rte = rt_fetch(var->varno, query->rtable); rte = rt_fetch(var->varno, query->rtable);
if (rte) if (rte)
{ {
Datum result; ItemPointer result;
Relation rel;
result = DirectFunctionCall2(currtid_byreloid, rel = table_open(rte->relid, AccessShareLock);
ObjectIdGetDatum(rte->relid), result = currtid_internal(rel, tid);
PointerGetDatum(tid)); table_close(rel, AccessShareLock);
table_close(viewrel, AccessShareLock);
return result; return result;
} }
} }
...@@ -352,56 +386,14 @@ currtid_for_view(Relation viewrel, ItemPointer tid) ...@@ -352,56 +386,14 @@ currtid_for_view(Relation viewrel, ItemPointer tid)
} }
} }
elog(ERROR, "currtid cannot handle this view"); elog(ERROR, "currtid cannot handle this view");
return (Datum) 0; return NULL;
}
Datum
currtid_byreloid(PG_FUNCTION_ARGS)
{
Oid reloid = PG_GETARG_OID(0);
ItemPointer tid = PG_GETARG_ITEMPOINTER(1);
ItemPointer result;
Relation rel;
AclResult aclresult;
Snapshot snapshot;
TableScanDesc scan;
result = (ItemPointer) palloc(sizeof(ItemPointerData));
if (!reloid)
{
*result = Current_last_tid;
PG_RETURN_ITEMPOINTER(result);
}
rel = table_open(reloid, AccessShareLock);
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
ACL_SELECT);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
RelationGetRelationName(rel));
if (rel->rd_rel->relkind == RELKIND_VIEW)
return currtid_for_view(rel, tid);
if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel));
ItemPointerCopy(tid, result);
snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = table_beginscan_tid(rel, snapshot);
table_tuple_get_latest_tid(scan, result);
table_endscan(scan);
UnregisterSnapshot(snapshot);
table_close(rel, AccessShareLock);
PG_RETURN_ITEMPOINTER(result);
} }
/*
* currtid_byrelname
* Get the latest tuple version of the tuple pointing at a CTID, for a
* given relation name.
*/
Datum Datum
currtid_byrelname(PG_FUNCTION_ARGS) currtid_byrelname(PG_FUNCTION_ARGS)
{ {
...@@ -410,35 +402,12 @@ currtid_byrelname(PG_FUNCTION_ARGS) ...@@ -410,35 +402,12 @@ currtid_byrelname(PG_FUNCTION_ARGS)
ItemPointer result; ItemPointer result;
RangeVar *relrv; RangeVar *relrv;
Relation rel; Relation rel;
AclResult aclresult;
Snapshot snapshot;
TableScanDesc scan;
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname)); relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
rel = table_openrv(relrv, AccessShareLock); rel = table_openrv(relrv, AccessShareLock);
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(), /* grab the latest tuple version associated to this CTID */
ACL_SELECT); result = currtid_internal(rel, tid);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, get_relkind_objtype(rel->rd_rel->relkind),
RelationGetRelationName(rel));
if (rel->rd_rel->relkind == RELKIND_VIEW)
return currtid_for_view(rel, tid);
if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel));
result = (ItemPointer) palloc(sizeof(ItemPointerData));
ItemPointerCopy(tid, result);
snapshot = RegisterSnapshot(GetLatestSnapshot());
scan = table_beginscan_tid(rel, snapshot);
table_tuple_get_latest_tid(scan, result);
table_endscan(scan);
UnregisterSnapshot(snapshot);
table_close(rel, AccessShareLock); table_close(rel, AccessShareLock);
......
...@@ -129,7 +129,6 @@ extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation, ...@@ -129,7 +129,6 @@ extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation,
bool *all_dead, bool first_call); bool *all_dead, bool first_call);
extern void heap_get_latest_tid(TableScanDesc scan, ItemPointer tid); extern void heap_get_latest_tid(TableScanDesc scan, ItemPointer tid);
extern void setLastTid(const ItemPointer tid);
extern BulkInsertState GetBulkInsertState(void); extern BulkInsertState GetBulkInsertState(void);
extern void FreeBulkInsertState(BulkInsertState); extern void FreeBulkInsertState(BulkInsertState);
......
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 202011241 #define CATALOG_VERSION_NO 202011251
#endif #endif
...@@ -2549,9 +2549,6 @@ ...@@ -2549,9 +2549,6 @@
{ oid => '1292', { oid => '1292',
proname => 'tideq', proleakproof => 't', prorettype => 'bool', proname => 'tideq', proleakproof => 't', prorettype => 'bool',
proargtypes => 'tid tid', prosrc => 'tideq' }, proargtypes => 'tid tid', prosrc => 'tideq' },
{ oid => '1293', descr => 'latest tid of a tuple',
proname => 'currtid', provolatile => 'v', proparallel => 'u',
prorettype => 'tid', proargtypes => 'oid tid', prosrc => 'currtid_byreloid' },
{ oid => '1294', descr => 'latest tid of a tuple', { oid => '1294', descr => 'latest tid of a tuple',
proname => 'currtid2', provolatile => 'v', proparallel => 'u', proname => 'currtid2', provolatile => 'v', proparallel => 'u',
prorettype => 'tid', proargtypes => 'text tid', prorettype => 'tid', proargtypes => 'text tid',
......
...@@ -15,21 +15,13 @@ SELECT max(ctid) FROM tid_tab; ...@@ -15,21 +15,13 @@ SELECT max(ctid) FROM tid_tab;
(1 row) (1 row)
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- Tests for currtid() and currtid2() with various relation kinds -- Tests for currtid2() with various relation kinds
-- Materialized view -- Materialized view
CREATE MATERIALIZED VIEW tid_matview AS SELECT a FROM tid_tab; CREATE MATERIALIZED VIEW tid_matview AS SELECT a FROM tid_tab;
SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: tid (0, 1) is not valid for relation "tid_matview"
SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- fails
ERROR: tid (0, 1) is not valid for relation "tid_matview" ERROR: tid (0, 1) is not valid for relation "tid_matview"
INSERT INTO tid_tab VALUES (1); INSERT INTO tid_tab VALUES (1);
REFRESH MATERIALIZED VIEW tid_matview; REFRESH MATERIALIZED VIEW tid_matview;
SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- ok
currtid
---------
(0,1)
(1 row)
SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- ok
currtid2 currtid2
---------- ----------
...@@ -40,12 +32,6 @@ DROP MATERIALIZED VIEW tid_matview; ...@@ -40,12 +32,6 @@ DROP MATERIALIZED VIEW tid_matview;
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- Sequence -- Sequence
CREATE SEQUENCE tid_seq; CREATE SEQUENCE tid_seq;
SELECT currtid('tid_seq'::regclass::oid, '(0,1)'::tid); -- ok
currtid
---------
(0,1)
(1 row)
SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok
currtid2 currtid2
---------- ----------
...@@ -55,39 +41,25 @@ SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok ...@@ -55,39 +41,25 @@ SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok
DROP SEQUENCE tid_seq; DROP SEQUENCE tid_seq;
-- Index, fails with incorrect relation type -- Index, fails with incorrect relation type
CREATE INDEX tid_ind ON tid_tab(a); CREATE INDEX tid_ind ON tid_tab(a);
SELECT currtid('tid_ind'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: "tid_ind" is an index
SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails
ERROR: "tid_ind" is an index ERROR: "tid_ind" is an index
DROP INDEX tid_ind; DROP INDEX tid_ind;
-- Partitioned table, no storage -- Partitioned table, no storage
CREATE TABLE tid_part (a int) PARTITION BY RANGE (a); CREATE TABLE tid_part (a int) PARTITION BY RANGE (a);
SELECT currtid('tid_part'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: cannot look at latest visible tid for relation "public.tid_part"
SELECT currtid2('tid_part'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_part'::text, '(0,1)'::tid); -- fails
ERROR: cannot look at latest visible tid for relation "public.tid_part" ERROR: cannot look at latest visible tid for relation "public.tid_part"
DROP TABLE tid_part; DROP TABLE tid_part;
-- Views -- Views
-- ctid not defined in the view -- ctid not defined in the view
CREATE VIEW tid_view_no_ctid AS SELECT a FROM tid_tab; CREATE VIEW tid_view_no_ctid AS SELECT a FROM tid_tab;
SELECT currtid('tid_view_no_ctid'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: currtid cannot handle views with no CTID
SELECT currtid2('tid_view_no_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_no_ctid'::text, '(0,1)'::tid); -- fails
ERROR: currtid cannot handle views with no CTID ERROR: currtid cannot handle views with no CTID
DROP VIEW tid_view_no_ctid; DROP VIEW tid_view_no_ctid;
-- ctid fetched directly from the source table. -- ctid fetched directly from the source table.
CREATE VIEW tid_view_with_ctid AS SELECT ctid, a FROM tid_tab; CREATE VIEW tid_view_with_ctid AS SELECT ctid, a FROM tid_tab;
SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: tid (0, 1) is not valid for relation "tid_tab"
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- fails
ERROR: tid (0, 1) is not valid for relation "tid_tab" ERROR: tid (0, 1) is not valid for relation "tid_tab"
INSERT INTO tid_tab VALUES (1); INSERT INTO tid_tab VALUES (1);
SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- ok
currtid
---------
(0,1)
(1 row)
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- ok
currtid2 currtid2
---------- ----------
...@@ -98,8 +70,6 @@ DROP VIEW tid_view_with_ctid; ...@@ -98,8 +70,6 @@ DROP VIEW tid_view_with_ctid;
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- ctid attribute with incorrect data type -- ctid attribute with incorrect data type
CREATE VIEW tid_view_fake_ctid AS SELECT 1 AS ctid, 2 AS a; CREATE VIEW tid_view_fake_ctid AS SELECT 1 AS ctid, 2 AS a;
SELECT currtid('tid_view_fake_ctid'::regclass::oid, '(0,1)'::tid); -- fails
ERROR: ctid isn't of type TID
SELECT currtid2('tid_view_fake_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_fake_ctid'::text, '(0,1)'::tid); -- fails
ERROR: ctid isn't of type TID ERROR: ctid isn't of type TID
DROP VIEW tid_view_fake_ctid; DROP VIEW tid_view_fake_ctid;
......
...@@ -8,55 +8,46 @@ SELECT min(ctid) FROM tid_tab; ...@@ -8,55 +8,46 @@ SELECT min(ctid) FROM tid_tab;
SELECT max(ctid) FROM tid_tab; SELECT max(ctid) FROM tid_tab;
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- Tests for currtid() and currtid2() with various relation kinds -- Tests for currtid2() with various relation kinds
-- Materialized view -- Materialized view
CREATE MATERIALIZED VIEW tid_matview AS SELECT a FROM tid_tab; CREATE MATERIALIZED VIEW tid_matview AS SELECT a FROM tid_tab;
SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- fails
INSERT INTO tid_tab VALUES (1); INSERT INTO tid_tab VALUES (1);
REFRESH MATERIALIZED VIEW tid_matview; REFRESH MATERIALIZED VIEW tid_matview;
SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- ok
SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- ok
DROP MATERIALIZED VIEW tid_matview; DROP MATERIALIZED VIEW tid_matview;
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- Sequence -- Sequence
CREATE SEQUENCE tid_seq; CREATE SEQUENCE tid_seq;
SELECT currtid('tid_seq'::regclass::oid, '(0,1)'::tid); -- ok
SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok
DROP SEQUENCE tid_seq; DROP SEQUENCE tid_seq;
-- Index, fails with incorrect relation type -- Index, fails with incorrect relation type
CREATE INDEX tid_ind ON tid_tab(a); CREATE INDEX tid_ind ON tid_tab(a);
SELECT currtid('tid_ind'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails
DROP INDEX tid_ind; DROP INDEX tid_ind;
-- Partitioned table, no storage -- Partitioned table, no storage
CREATE TABLE tid_part (a int) PARTITION BY RANGE (a); CREATE TABLE tid_part (a int) PARTITION BY RANGE (a);
SELECT currtid('tid_part'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_part'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_part'::text, '(0,1)'::tid); -- fails
DROP TABLE tid_part; DROP TABLE tid_part;
-- Views -- Views
-- ctid not defined in the view -- ctid not defined in the view
CREATE VIEW tid_view_no_ctid AS SELECT a FROM tid_tab; CREATE VIEW tid_view_no_ctid AS SELECT a FROM tid_tab;
SELECT currtid('tid_view_no_ctid'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_view_no_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_no_ctid'::text, '(0,1)'::tid); -- fails
DROP VIEW tid_view_no_ctid; DROP VIEW tid_view_no_ctid;
-- ctid fetched directly from the source table. -- ctid fetched directly from the source table.
CREATE VIEW tid_view_with_ctid AS SELECT ctid, a FROM tid_tab; CREATE VIEW tid_view_with_ctid AS SELECT ctid, a FROM tid_tab;
SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- fails
INSERT INTO tid_tab VALUES (1); INSERT INTO tid_tab VALUES (1);
SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- ok
SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- ok SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- ok
DROP VIEW tid_view_with_ctid; DROP VIEW tid_view_with_ctid;
TRUNCATE tid_tab; TRUNCATE tid_tab;
-- ctid attribute with incorrect data type -- ctid attribute with incorrect data type
CREATE VIEW tid_view_fake_ctid AS SELECT 1 AS ctid, 2 AS a; CREATE VIEW tid_view_fake_ctid AS SELECT 1 AS ctid, 2 AS a;
SELECT currtid('tid_view_fake_ctid'::regclass::oid, '(0,1)'::tid); -- fails
SELECT currtid2('tid_view_fake_ctid'::text, '(0,1)'::tid); -- fails SELECT currtid2('tid_view_fake_ctid'::text, '(0,1)'::tid); -- fails
DROP VIEW tid_view_fake_ctid; DROP VIEW tid_view_fake_ctid;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment