1-- tests for functions related to TID handling
2CREATE TABLE tid_tab (a int);
3-- min() and max() for TIDs
4INSERT INTO tid_tab VALUES (1), (2);
5SELECT min(ctid) FROM tid_tab;
6  min
7-------
8 (0,1)
9(1 row)
10
11SELECT max(ctid) FROM tid_tab;
12  max
13-------
14 (0,2)
15(1 row)
16
17TRUNCATE tid_tab;
18-- Tests for currtid() and currtid2() with various relation kinds
19-- Materialized view
20CREATE MATERIALIZED VIEW tid_matview AS SELECT a FROM tid_tab;
21SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- fails
22ERROR:  tid (0, 1) is not valid for relation "tid_matview"
23SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- fails
24ERROR:  tid (0, 1) is not valid for relation "tid_matview"
25INSERT INTO tid_tab VALUES (1);
26REFRESH MATERIALIZED VIEW tid_matview;
27SELECT currtid('tid_matview'::regclass::oid, '(0,1)'::tid); -- ok
28 currtid
29---------
30 (0,1)
31(1 row)
32
33SELECT currtid2('tid_matview'::text, '(0,1)'::tid); -- ok
34 currtid2
35----------
36 (0,1)
37(1 row)
38
39DROP MATERIALIZED VIEW tid_matview;
40TRUNCATE tid_tab;
41-- Sequence
42CREATE SEQUENCE tid_seq;
43SELECT currtid('tid_seq'::regclass::oid, '(0,1)'::tid); -- ok
44 currtid
45---------
46 (0,1)
47(1 row)
48
49SELECT currtid2('tid_seq'::text, '(0,1)'::tid); -- ok
50 currtid2
51----------
52 (0,1)
53(1 row)
54
55DROP SEQUENCE tid_seq;
56-- Index, fails with incorrect relation type
57CREATE INDEX tid_ind ON tid_tab(a);
58SELECT currtid('tid_ind'::regclass::oid, '(0,1)'::tid); -- fails
59ERROR:  "tid_ind" is an index
60SELECT currtid2('tid_ind'::text, '(0,1)'::tid); -- fails
61ERROR:  "tid_ind" is an index
62DROP INDEX tid_ind;
63-- Partitioned table, no storage
64CREATE TABLE tid_part (a int) PARTITION BY RANGE (a);
65SELECT currtid('tid_part'::regclass::oid, '(0,1)'::tid); -- fails
66ERROR:  cannot look at latest visible tid for relation "public.tid_part"
67SELECT currtid2('tid_part'::text, '(0,1)'::tid); -- fails
68ERROR:  cannot look at latest visible tid for relation "public.tid_part"
69DROP TABLE tid_part;
70-- Views
71-- ctid not defined in the view
72CREATE VIEW tid_view_no_ctid AS SELECT a FROM tid_tab;
73SELECT currtid('tid_view_no_ctid'::regclass::oid, '(0,1)'::tid); -- fails
74ERROR:  currtid cannot handle views with no CTID
75SELECT currtid2('tid_view_no_ctid'::text, '(0,1)'::tid); -- fails
76ERROR:  currtid cannot handle views with no CTID
77DROP VIEW tid_view_no_ctid;
78-- ctid fetched directly from the source table.
79CREATE VIEW tid_view_with_ctid AS SELECT ctid, a FROM tid_tab;
80SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- fails
81ERROR:  tid (0, 1) is not valid for relation "tid_tab"
82SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- fails
83ERROR:  tid (0, 1) is not valid for relation "tid_tab"
84INSERT INTO tid_tab VALUES (1);
85SELECT currtid('tid_view_with_ctid'::regclass::oid, '(0,1)'::tid); -- ok
86 currtid
87---------
88 (0,1)
89(1 row)
90
91SELECT currtid2('tid_view_with_ctid'::text, '(0,1)'::tid); -- ok
92 currtid2
93----------
94 (0,1)
95(1 row)
96
97DROP VIEW tid_view_with_ctid;
98TRUNCATE tid_tab;
99-- ctid attribute with incorrect data type
100CREATE VIEW tid_view_fake_ctid AS SELECT 1 AS ctid, 2 AS a;
101SELECT currtid('tid_view_fake_ctid'::regclass::oid, '(0,1)'::tid); -- fails
102ERROR:  ctid isn't of type TID
103SELECT currtid2('tid_view_fake_ctid'::text, '(0,1)'::tid); -- fails
104ERROR:  ctid isn't of type TID
105DROP VIEW tid_view_fake_ctid;
106DROP TABLE tid_tab CASCADE;
107