Skip to content

Commit 7a2dd70

Browse files
authored
internal/dinosql: Support the pg_temp schema (#183)
The schema is removed from the catalog so that queries can not reference it, since it will be empty in a new session.
1 parent 80852e7 commit 7a2dd70

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

internal/catalog/build_test.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,27 @@ func TestUpdate(t *testing.T) {
481481
},
482482
},
483483
},
484+
{
485+
`
486+
CREATE TABLE pg_temp.migrate (val INT);
487+
INSERT INTO pg_temp.migrate (val) SELECT val FROM old;
488+
INSERT INTO new (val) SELECT val FROM pg_temp.migrate;
489+
`,
490+
pg.Catalog{
491+
Schemas: map[string]pg.Schema{
492+
"pg_temp": {
493+
Tables: map[string]pg.Table{
494+
"migrate": pg.Table{
495+
Name: "migrate",
496+
Columns: []pg.Column{
497+
{Name: "val", DataType: "pg_catalog.int4", NotNull: false, Table: pg.FQN{Schema: "pg_temp", Rel: "migrate"}},
498+
},
499+
},
500+
},
501+
},
502+
},
503+
},
504+
},
484505
} {
485506
test := tc
486507
t.Run(strconv.Itoa(i), func(t *testing.T) {
@@ -490,10 +511,12 @@ func TestUpdate(t *testing.T) {
490511
t.Fatal(err)
491512
}
492513

493-
delete(c.Schemas, "pg_catalog")
494-
delete(test.c.Schemas, "pg_catalog")
514+
expected := pg.NewCatalog()
515+
for name, schema := range test.c.Schemas {
516+
expected.Schemas[name] = schema
517+
}
495518

496-
if diff := cmp.Diff(test.c, c, cmpopts.EquateEmpty()); diff != "" {
519+
if diff := cmp.Diff(expected, c, cmpopts.EquateEmpty()); diff != "" {
497520
t.Log(test.stmt)
498521
t.Errorf("catalog mismatch:\n%s", diff)
499522
}

internal/dinosql/parser.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ func ParseCatalog(schema string) (core.Catalog, error) {
108108
}
109109
}
110110
}
111+
112+
// The pg_temp schema is scoped to the current session. Remove it from the
113+
// catalog so that other queries can not read from it.
114+
delete(c.Schemas, "pg_temp")
115+
111116
if len(merr.Errs) > 0 {
112117
return c, merr
113118
}

internal/pg/catalog.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ func NewCatalog() Catalog {
55
Schemas: map[string]Schema{
66
"public": NewSchema(),
77
"pg_catalog": pgCatalog(),
8+
// Likewise, the current session's temporary-table schema, pg_temp_nnn, is
9+
// always searched if it exists. It can be explicitly listed in the path by
10+
// using the alias pg_temp. If it is not listed in the path then it is
11+
// searched first (even before pg_catalog). However, the temporary schema is
12+
// only searched for relation (table, view, sequence, etc) and data type
13+
// names. It is never searched for function or operator names.
14+
//
15+
// https://www.postgresql.org/docs/current/runtime-config-client.html
16+
"pg_temp": NewSchema(),
817
},
918
}
1019
}

0 commit comments

Comments
 (0)