Skip to content

Commit 4c451e0

Browse files
committed
Fix rename and reindex bugs when objects are not relations
When issuing rename or reindex commands that do not involve relations, the relation rangevar in the utility statement will be NULL. This change adds extra checks to avoid calling, e.g., RangeVarGetRelid() when a relation's rangevar is NULL, as that function does not accept NULL input values.
1 parent c3ebc67 commit 4c451e0

File tree

3 files changed

+117
-13
lines changed

3 files changed

+117
-13
lines changed

src/process_utility.c

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,17 @@ process_truncate(Node *parsetree)
148148

149149
foreach(cell, truncatestmt->relations)
150150
{
151-
Oid relId = RangeVarGetRelid(lfirst(cell), NoLock, true);
151+
RangeVar *relation = lfirst(cell);
152+
Oid relid;
152153

153-
if (OidIsValid(relId))
154+
if (NULL == relation)
155+
continue;
156+
157+
relid = RangeVarGetRelid(relation, NoLock, true);
158+
159+
if (OidIsValid(relid))
154160
{
155-
Hypertable *ht = hypertable_cache_get_entry(hcache, relId);
161+
Hypertable *ht = hypertable_cache_get_entry(hcache, relid);
156162

157163
if (ht != NULL)
158164
{
@@ -172,7 +178,7 @@ process_alterobjectschema(Node *parsetree)
172178
Cache *hcache;
173179
Hypertable *ht;
174180

175-
if (alterstmt->objectType != OBJECT_TABLE)
181+
if (alterstmt->objectType != OBJECT_TABLE || NULL == alterstmt->relation)
176182
return;
177183

178184
relid = RangeVarGetRelid(alterstmt->relation, NoLock, true);
@@ -202,7 +208,7 @@ process_copy(Node *parsetree, const char *query_string, char *completion_tag)
202208
Cache *hcache;
203209
Oid relid;
204210

205-
if (!stmt->is_from)
211+
if (!stmt->is_from || NULL == stmt->relation)
206212
return false;
207213

208214
relid = RangeVarGetRelid(stmt->relation, NoLock, true);
@@ -326,7 +332,13 @@ process_drop_table(DropStmt *stmt)
326332
foreach(lc, stmt->objects)
327333
{
328334
List *object = lfirst(lc);
329-
Oid relid = RangeVarGetRelid(makeRangeVarFromNameList(object), NoLock, true);
335+
RangeVar *relation = makeRangeVarFromNameList(object);
336+
Oid relid;
337+
338+
if (NULL == relation)
339+
continue;
340+
341+
relid = RangeVarGetRelid(relation, NoLock, true);
330342

331343
if (OidIsValid(relid))
332344
{
@@ -428,8 +440,13 @@ process_drop_index(DropStmt *stmt)
428440
foreach(lc, stmt->objects)
429441
{
430442
List *object = lfirst(lc);
431-
RangeVar *rv = makeRangeVarFromNameList(object);
432-
Oid idxrelid = RangeVarGetRelid(rv, NoLock, true);
443+
RangeVar *relation = makeRangeVarFromNameList(object);
444+
Oid idxrelid;
445+
446+
if (NULL == relation)
447+
continue;
448+
449+
idxrelid = RangeVarGetRelid(relation, NoLock, true);
433450

434451
if (OidIsValid(idxrelid))
435452
{
@@ -511,7 +528,16 @@ static bool
511528
process_reindex(Node *parsetree)
512529
{
513530
ReindexStmt *stmt = (ReindexStmt *) parsetree;
514-
Oid relid = RangeVarGetRelid(stmt->relation, NoLock, true);
531+
Oid relid;
532+
533+
if (NULL == stmt->relation)
534+
/* Not a case we are interested in */
535+
return false;
536+
537+
relid = RangeVarGetRelid(stmt->relation, NoLock, true);
538+
539+
if (!OidIsValid(relid))
540+
return false;
515541

516542
switch (stmt->kind)
517543
{
@@ -571,8 +597,13 @@ process_rename_column(Cache *hcache, Oid relid, RenameStmt *stmt)
571597
static void
572598
process_rename_index(Cache *hcache, Oid relid, RenameStmt *stmt)
573599
{
574-
Oid tablerelid = IndexGetRelation(relid, false);
575-
Hypertable *ht = hypertable_cache_get_entry(hcache, tablerelid);
600+
Oid tablerelid = IndexGetRelation(relid, true);
601+
Hypertable *ht;
602+
603+
if (!OidIsValid(tablerelid))
604+
return;
605+
606+
ht = hypertable_cache_get_entry(hcache, tablerelid);
576607

577608
if (NULL != ht)
578609
chunk_index_rename_parent(ht, relid, stmt->newname);
@@ -589,14 +620,20 @@ static void
589620
process_rename(Node *parsetree)
590621
{
591622
RenameStmt *stmt = (RenameStmt *) parsetree;
592-
Oid relid = RangeVarGetRelid(stmt->relation, NoLock, true);
623+
Oid relid;
593624
Cache *hcache;
594625

595-
/* TODO: forbid all rename op on chunk table */
626+
if (NULL == stmt->relation)
627+
/* Not an object we are interested in */
628+
return;
629+
630+
relid = RangeVarGetRelid(stmt->relation, NoLock, true);
596631

597632
if (!OidIsValid(relid))
598633
return;
599634

635+
/* TODO: forbid all rename op on chunk table */
636+
600637
hcache = hypertable_cache_pin();
601638

602639
switch (stmt->renameType)

test/expected/plain.out

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
\ir include/create_single_db.sql
2+
SET client_min_messages = WARNING;
3+
DROP DATABASE IF EXISTS single;
4+
SET client_min_messages = NOTICE;
5+
CREATE DATABASE single;
6+
\c single
7+
CREATE EXTENSION IF NOT EXISTS timescaledb;
8+
SET timescaledb.disable_optimizations = :DISABLE_OPTIMIZATIONS;
9+
-- Tests for plain PostgreSQL commands to ensure that they work while
10+
-- the TimescaleDB extension is loaded. This is a mix of statements
11+
-- added mostly as regression checks when bugs are discovered and
12+
-- fixed.
13+
CREATE TABLE regular_table(time timestamp, temp float8, tag text, color integer);
14+
-- Renaming indexes should work
15+
CREATE INDEX time_color_idx ON regular_table(time, color);
16+
ALTER INDEX time_color_idx RENAME TO time_color_idx2;
17+
\d+ regular_table
18+
Table "public.regular_table"
19+
Column | Type | Modifiers | Storage | Stats target | Description
20+
--------+-----------------------------+-----------+----------+--------------+-------------
21+
time | timestamp without time zone | | plain | |
22+
temp | double precision | | plain | |
23+
tag | text | | extended | |
24+
color | integer | | plain | |
25+
Indexes:
26+
"time_color_idx2" btree ("time", color)
27+
28+
-- Renaming types should work
29+
CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
30+
ALTER TYPE rainbow RENAME TO colors;
31+
\dT+
32+
List of data types
33+
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
34+
--------+--------+---------------+------+----------+----------+-------------------+-------------
35+
public | colors | colors | 4 | red +| postgres | |
36+
| | | | orange +| | |
37+
| | | | yellow +| | |
38+
| | | | green +| | |
39+
| | | | blue +| | |
40+
| | | | purple | | |
41+
(1 row)
42+
43+
REINDEX TABLE regular_table;
44+
REINDEX SCHEMA public;

test/sql/plain.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
\ir include/create_single_db.sql
2+
3+
-- Tests for plain PostgreSQL commands to ensure that they work while
4+
-- the TimescaleDB extension is loaded. This is a mix of statements
5+
-- added mostly as regression checks when bugs are discovered and
6+
-- fixed.
7+
8+
CREATE TABLE regular_table(time timestamp, temp float8, tag text, color integer);
9+
10+
-- Renaming indexes should work
11+
CREATE INDEX time_color_idx ON regular_table(time, color);
12+
ALTER INDEX time_color_idx RENAME TO time_color_idx2;
13+
14+
\d+ regular_table
15+
16+
-- Renaming types should work
17+
CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
18+
ALTER TYPE rainbow RENAME TO colors;
19+
20+
\dT+
21+
22+
REINDEX TABLE regular_table;
23+
REINDEX SCHEMA public;

0 commit comments

Comments
 (0)