-- Regular index with included columns CREATETABLE tbl_include_reg (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_reg SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATEINDEX tbl_include_reg_idx ON tbl_include_reg (c1, c2) INCLUDE (c3, c4); -- duplicate column is pretty pointless, but we allow it anyway CREATEINDEXON tbl_include_reg (c1, c2) INCLUDE (c1, c3); SELECT pg_get_indexdef(i.indexrelid) FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid WHERE i.indrelid = 'tbl_include_reg'::regclass ORDERBY c.relname;
\d tbl_include_reg_idx
-- Unique index and unique constraint CREATETABLE tbl_include_unique1 (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_unique1 SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATEUNIQUEINDEX tbl_include_unique1_idx_unique ON tbl_include_unique1 using btree (c1, c2) INCLUDE (c3, c4); ALTERTABLE tbl_include_unique1 addUNIQUEUSINGINDEX tbl_include_unique1_idx_unique; ALTERTABLE tbl_include_unique1 addUNIQUE (c1, c2) INCLUDE (c3, c4); SELECT pg_get_indexdef(i.indexrelid) FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid WHERE i.indrelid = 'tbl_include_unique1'::regclass ORDERBY c.relname;
-- Unique index and unique constraint. Both must fail. CREATETABLE tbl_include_unique2 (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_unique2 SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATEUNIQUEINDEX tbl_include_unique2_idx_unique ON tbl_include_unique2 using btree (c1, c2) INCLUDE (c3, c4); ALTERTABLE tbl_include_unique2 addUNIQUE (c1, c2) INCLUDE (c3, c4);
-- PK constraint CREATETABLE tbl_include_pk (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_pk SELECT1, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; ALTERTABLE tbl_include_pk addPRIMARYKEY (c1, c2) INCLUDE (c3, c4); SELECT pg_get_indexdef(i.indexrelid) FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid WHERE i.indrelid = 'tbl_include_pk'::regclass ORDERBY c.relname;
CREATETABLE tbl_include_box (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_box SELECT1, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATEUNIQUEINDEX tbl_include_box_idx_unique ON tbl_include_box using btree (c1, c2) INCLUDE (c3, c4); ALTERTABLE tbl_include_box addPRIMARYKEYUSINGINDEX tbl_include_box_idx_unique; SELECT pg_get_indexdef(i.indexrelid) FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid WHERE i.indrelid = 'tbl_include_box'::regclass ORDERBY c.relname;
-- PK constraint. Must fail. CREATETABLE tbl_include_box_pk (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl_include_box_pk SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; ALTERTABLE tbl_include_box_pk addPRIMARYKEY (c1, c2) INCLUDE (c3, c4);
/* *2.TestCREATETABLEwithconstraint
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, CONSTRAINT covering UNIQUE(c1,c2) INCLUDE(c3,c4)); SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid; SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid; -- ensure that constraint works INSERTINTO tbl SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; DROPTABLE tbl;
CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, CONSTRAINT covering PRIMARYKEY(c1,c2) INCLUDE(c3,c4)); SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid; SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid AND contype = 'p'; -- ensure that constraint works INSERTINTO tbl SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; INSERTINTO tbl SELECT1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; INSERTINTO tbl SELECT x, 2*x, NULL, NULLFROM generate_series(1,300) AS x; explain (costs off) select * from tbl where (c1,c2,c3) < (2,5,1); select * from tbl where (c1,c2,c3) < (2,5,1); -- row comparison that compares high key at page boundary SET enable_seqscan = off; explain (costs off) select * from tbl where (c1,c2,c3) < (262,1,1) limit1; select * from tbl where (c1,c2,c3) < (262,1,1) limit1; DROPTABLE tbl;
RESET enable_seqscan;
CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, UNIQUE(c1,c2) INCLUDE(c3,c4)); SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid; SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid; -- ensure that constraint works INSERTINTO tbl SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; DROPTABLE tbl;
CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, PRIMARYKEY(c1,c2) INCLUDE(c3,c4)); SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid; SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid AND contype = 'p'; -- ensure that constraint works INSERTINTO tbl SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; INSERTINTO tbl SELECT1, NULL, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; INSERTINTO tbl SELECT x, 2*x, NULL, NULLFROM generate_series(1,10) AS x; DROPTABLE tbl;
CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box,
EXCLUDE USING btree (c1 WITH =) INCLUDE(c3,c4)); SELECT indexrelid::regclass, indnatts, indnkeyatts, indisunique, indisprimary, indkey, indclass FROM pg_index WHERE indrelid = 'tbl'::regclass::oid; SELECT pg_get_constraintdef(oid), conname, conkey FROM pg_constraint WHERE conrelid = 'tbl'::regclass::oid; -- ensure that constraint works INSERTINTO tbl SELECT1, 2, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; INSERTINTO tbl SELECT x, 2*x, NULL, NULLFROM generate_series(1,10) AS x; DROPTABLE tbl;
/* *3.0TestALTERTABLEDROPCOLUMN. *Anycolumndeletionleadstoindexdeletion.
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 int); CREATEUNIQUEINDEX tbl_idx ON tbl using btree(c1, c2, c3, c4); SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c3; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; DROPTABLE tbl;
/* *3.1TestALTERTABLEDROPCOLUMN. *Includedcolumndeletionleadstotheindexdeletion, *ASwellASkeycolumnsdeletion.It'sexplainedindocumentation.
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box); CREATEUNIQUEINDEX tbl_idx ON tbl using btree(c1, c2) INCLUDE(c3,c4); SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c3; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; DROPTABLE tbl;
/* *3.2TestALTERTABLEDROPCOLUMN. *Includedcolumndeletionleadstotheindexdeletion. *ASwellASkeycolumnsdeletion.It'sexplainedindocumentation.
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, UNIQUE(c1, c2) INCLUDE(c3,c4)); SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c3; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c1; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; DROPTABLE tbl;
/* *4.CREATEINDEXCONCURRENTLY
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, UNIQUE(c1, c2) INCLUDE(c3,c4)); INSERTINTO tbl SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,1000) AS x; CREATEUNIQUEINDEX CONCURRENTLY on tbl (c1, c2) INCLUDE (c3, c4); SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; DROPTABLE tbl;
/* *5.REINDEX
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, UNIQUE(c1, c2) INCLUDE(c3,c4)); SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c3; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname;
REINDEX INDEX tbl_c1_c2_c3_c4_key; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; ALTERTABLE tbl DROPCOLUMN c1; SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl'ORDERBY indexname; DROPTABLE tbl;
/* *7.CheckvariousAMs.Allbutbtree,gistandspgistmustfail.
*/ CREATETABLE tbl (c1 int,c2 int, c3 box, c4 box); CREATEINDEXon tbl USING brin(c1, c2) INCLUDE (c3, c4); CREATEINDEXon tbl USING gist(c3) INCLUDE (c1, c4); CREATEINDEXon tbl USING spgist(c3) INCLUDE (c4); CREATEINDEXon tbl USING gin(c1, c2) INCLUDE (c3, c4); CREATEINDEXon tbl USING hash(c1, c2) INCLUDE (c3, c4); CREATEINDEXon tbl USING rtree(c3) INCLUDE (c1, c4); CREATEINDEXon tbl USING btree(c1, c2) INCLUDE (c3, c4); DROPTABLE tbl;
/* *8.Update,deletevaluesinindexedtable.
*/ CREATETABLE tbl (c1 int, c2 int, c3 int, c4 box); INSERTINTO tbl SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; CREATEUNIQUEINDEX tbl_idx_unique ON tbl using btree(c1, c2) INCLUDE (c3,c4); UPDATE tbl SET c1 = 100WHERE c1 = 2; UPDATE tbl SET c1 = 1WHERE c1 = 3; -- should fail UPDATE tbl SET c2 = 2WHERE c1 = 1; UPDATE tbl SET c3 = 1; DELETEFROM tbl WHERE c1 = 5OR c3 = 12; DROPTABLE tbl;
/* *9.Altercolumntype.
*/ CREATETABLE tbl (c1 int,c2 int, c3 int, c4 box, UNIQUE(c1, c2) INCLUDE(c3,c4)); INSERTINTO tbl SELECT x, 2*x, 3*x, box('4,4,4,4') FROM generate_series(1,10) AS x; ALTERTABLE tbl ALTER c1 TYPE bigint; ALTERTABLE tbl ALTER c3 TYPE bigint;
\d tbl DROPTABLE tbl;
/* *10.Testcoveragefornamesstoredascstringsinindexes
*/ CREATETABLE nametbl (c1 int, c2 name, c3 float); CREATEINDEX nametbl_c1_c2_idx ON nametbl (c2, c1) INCLUDE (c3); INSERTINTO nametbl VALUES(1, 'two', 3.0);
VACUUM nametbl; SET enable_seqscan = 0;
-- Ensure we get an index only scan plan EXPLAIN (COSTS OFF) SELECT c2, c1, c3 FROM nametbl WHERE c2 = 'two'AND c1 = 1;
-- Validate the results look sane SELECT c2, c1, c3 FROM nametbl WHERE c2 = 'two'AND c1 = 1;
RESET enable_seqscan;
DROPTABLE nametbl;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.