|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += pgtt |
| 5 | + |
| 6 | +== Overview |
| 7 | +pgtt is a PostgreSQL extension to create, manage and use Oracle-style Global Temporary Tables. |
| 8 | +The objective of this extension it to provide the Global Temporary Table feature to PostgreSQL waiting for an in core implementation |
| 9 | + |
| 10 | +== Installation |
| 11 | + |
| 12 | +[TIP] |
| 13 | +The source installation environment is Ubuntu 24.04 (x86_64), with IvorySQL already installed in the environment at /path-to/ivorysql |
| 14 | + |
| 15 | +=== Source Installation |
| 16 | + |
| 17 | +[literal] |
| 18 | +---- |
| 19 | +# Download pgtt source code |
| 20 | +wget https://github.com/darold/pgtt/archive/refs/tags/v4.5.tar.gz |
| 21 | +tar zxvf v4.5.tar.gz |
| 22 | +cd pgtt-4.5 |
| 23 | +
|
| 24 | +# Compile and install |
| 25 | +make PG_CONFIF=/path-to/ivorysql/bin/pg_config |
| 26 | +make PG_CONFIF=/path-to/ivorysql/bin/pg_config install |
| 27 | +---- |
| 28 | + |
| 29 | +== Create Extension |
| 30 | + |
| 31 | +Connect to the database using psql and execute the following command: |
| 32 | +[literal] |
| 33 | +---- |
| 34 | +-- create the extension |
| 35 | +CREATE EXTENSION pgtt; |
| 36 | +---- |
| 37 | + |
| 38 | +== Load the Extension |
| 39 | + |
| 40 | +[literal] |
| 41 | +---- |
| 42 | +After creating the extension, it needs to be loaded before use |
| 43 | +You can use one of the following three methods to load it: |
| 44 | +
|
| 45 | +1. Modify session_preload_libraries in the configuration file |
| 46 | +session_preload_libraries = 'pgtt' |
| 47 | +
|
| 48 | +2. Enable at database level |
| 49 | +ALTER DATABASE mydb SET session_preload_libraries = 'pgtt'; |
| 50 | +
|
| 51 | +3. Load in session |
| 52 | +LOAD 'pgtt'; |
| 53 | +
|
| 54 | +After successfully loading the extension, you can see that the value of pgtt.enabled is on |
| 55 | +ivorysql=# show pgtt.enabled; |
| 56 | + pgtt.enabled |
| 57 | +-------------- |
| 58 | + on |
| 59 | +(1 row) |
| 60 | + |
| 61 | +pgtt_schema is added to search_path |
| 62 | +ivorysql=# SHOW search_path; |
| 63 | + search_path |
| 64 | +------------------------------ |
| 65 | + "$user", public, pgtt_schema |
| 66 | +(1 row) |
| 67 | +
|
| 68 | +---- |
| 69 | + |
| 70 | +== Usage |
| 71 | + |
| 72 | +[literal] |
| 73 | +---- |
| 74 | +Create temporary table: |
| 75 | +Using this statement will produce a warning message, but it can be ignored. |
| 76 | +
|
| 77 | +ivorysql=# CREATE GLOBAL TEMPORARY TABLE test_gtt_table ( |
| 78 | + id integer, |
| 79 | + lbl text |
| 80 | +) ON COMMIT PRESERVE ROWS; |
| 81 | +WARNING: GLOBAL is deprecated in temporary table creation |
| 82 | +LINE 1: CREATE GLOBAL TEMPORARY TABLE test_gtt_table ( |
| 83 | + ^ |
| 84 | +CREATE TABLE |
| 85 | +
|
| 86 | +If you don't want to produce this warning message, you can use comment symbols: |
| 87 | +CREATE /*GLOBAL*/ TEMPORARY TABLE test_gtt_table ( |
| 88 | + id integer, |
| 89 | + lbl text |
| 90 | +) ON COMMIT PRESERVE ROWS; |
| 91 | +
|
| 92 | +
|
| 93 | +Temporary tables can also be created using the LIKE clause: |
| 94 | +ivorysql=# CREATE /*GLOBAL*/ TEMPORARY TABLE test_gtt_table AS SELECT * FROM source_table WITH DATA; |
| 95 | +CREATE TABLE AS |
| 96 | +
|
| 97 | +Create indexes on table columns: |
| 98 | +ivorysql=# CREATE INDEX ON test_gtt_table (id); |
| 99 | +CREATE INDEX |
| 100 | +
|
| 101 | +Drop temporary table: |
| 102 | +ivorysql=# drop table test_gtt_table; |
| 103 | +DROP TABLE |
| 104 | +
|
| 105 | +Add constraints (except foreign keys): |
| 106 | +ivorysql=# CREATE /*GLOBAL*/ TEMPORARY TABLE t2 ( |
| 107 | + c1 serial PRIMARY KEY, |
| 108 | + c2 VARCHAR (50) UNIQUE NOT NULL, |
| 109 | + c3 boolean DEFAULT false |
| 110 | +); |
| 111 | +CREATE TABLE |
| 112 | +
|
| 113 | +Creating tables with foreign keys is not allowed: |
| 114 | +ivorysql=# CREATE /*GLOBAL*/ TEMPORARY TABLE t1 (c1 integer, FOREIGN KEY (c1) REFERENCES source (id)); |
| 115 | +ERROR: attempt to create referential integrity constraint on global temporary table |
| 116 | +
|
| 117 | +ivorysql=# ALTER TABLE t2 ADD FOREIGN KEY (c1) REFERENCES source (id); |
| 118 | +ERROR: attempt to create referential integrity constraint on global temporary table |
| 119 | +---- |
| 120 | + |
| 121 | +For more detailed usage and advanced features, please refer to https://github.com/amutu/pgtt . |
| 122 | + |
0 commit comments