Skip to content

Commit 143eef3

Browse files
craig[bot]rafiss
andcommitted
Merge #145005
145005: sql: improve error message for creating type in pg_temp r=rafiss a=rafiss We do not support temporary types. We don't support the syntax for it, but one other way users can try to do this is by specifying the pg_temp schema. This patch adds a nicer error message when this is attempted, rather than the current behavior to show an internal assertion error. fixes #142780 Release note: None Co-authored-by: Rafi Shamim <[email protected]>
2 parents 13229ac + 3c24d0d commit 143eef3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pkg/sql/create_type.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package sql
88
import (
99
"context"
1010
"fmt"
11+
"strings"
1112

1213
"github.com/cockroachdb/cockroach/pkg/keys"
1314
"github.com/cockroachdb/cockroach/pkg/kv"
@@ -107,6 +108,14 @@ func (n *createTypeNode) startExec(params runParams) error {
107108
func resolveNewTypeName(
108109
ctx context.Context, p *planner, name *tree.UnresolvedObjectName,
109110
) (*tree.TypeName, catalog.DatabaseDescriptor, error) {
111+
// Check that we are not creating in a temporary schema. To support this,
112+
// we'd need to update the type descriptor to have an IsTemporary flag, and
113+
// the temporary object cleaner would need to handle these.
114+
if name.HasExplicitSchema() && strings.HasPrefix(name.Schema(), catconstants.PgTempSchemaName) {
115+
return nil, nil, pgerror.Newf(pgcode.InvalidSchemaName,
116+
"cannot create type %q in temporary schema", name.Object(),
117+
)
118+
}
110119
// Resolve the target schema and database.
111120
db, _, prefix, err := p.ResolveTargetObject(ctx, name)
112121
if err != nil {

pkg/sql/logictest/testdata/logic_test/temp_table

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,33 @@ statement ok
481481
DROP DATABASE database_108751 CASCADE
482482

483483
subtest end
484+
485+
# Verify that a good error message is returned for temporary types.
486+
subtest temp_type
487+
488+
statement ok
489+
CREATE DATABASE database_142780
490+
491+
statement ok
492+
USE database_142780
493+
494+
statement ok
495+
SET experimental_enable_temp_tables = on
496+
497+
statement error cannot create type "temp_type" in temporary schema
498+
CREATE TYPE pg_temp.temp_type AS (a int, b int);
499+
500+
# Verify that the error message is also good if we create a temporary table first.
501+
statement ok
502+
CREATE TEMP TABLE temp_table_142780 (a int primary key, b int);
503+
504+
statement error cannot create type "temp_type" in temporary schema
505+
CREATE TYPE pg_temp.temp_type AS (a int, b int);
506+
507+
statement ok
508+
RESET database
509+
510+
statement ok
511+
DROP DATABASE database_142780
512+
513+
subtest end

0 commit comments

Comments
 (0)