Skip to content

Commit 54b4b03

Browse files
committed
Handle better the hint table if the extension is not created
An environment enabling pg_hint_plan.enable_hint_table while pg_hint_plan has not been created as an extension would fail all its queries as the table created by the extension does not exist. This commit adds a check at the top of get_hints_from_table() to check if the extension is installed before attempting to use the hint table, generating a WARNING. We have discussed about a few options, but a WARNING is more consistent with how duplicated hints or compute_query_id disabled are handled. This does not completely close the failure hole, though, as it is still possible to see the table lookup failure for the CREATE EXTENSION command enabling pg_hint_plan as an extension if enable_hint_table is enabled. In terms of code simplicity, I am not really convinced that we need to do more just for that. This commit relies on 490f869d92e5 that has introduced syscache entries for pg_extension. On stable branches, we will need a different logic with a check on the table itself with its namespace. Idea based on some feedback from Julien Rouhaud. The tests are from me. Author: Sami Imseih, Michael Paquier Backpatch-through: 12 Per issue #191.
1 parent 0347b9d commit 54b4b03

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ MODULES = pg_hint_plan
88
HINTPLANVER = 1.6.1
99

1010
REGRESS = init base_plan pg_hint_plan ut-init ut-A ut-S ut-J ut-L ut-G ut-R \
11-
ut-fdw ut-W ut-T ut-fini hints_anywhere plpgsql oldextversions
11+
ut-fdw ut-W ut-T ut-fini hints_anywhere plpgsql hint_table oldextversions
1212
REGRESS_OPTS = --encoding=UTF8
1313

1414
EXTENSION = pg_hint_plan

expected/hint_table.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Tests for the hint table
2+
LOAD 'pg_hint_plan';
3+
-- Attempting to use the hint table without the extension created
4+
-- emits a WARNING.
5+
SET pg_hint_plan.enable_hint_table TO on;
6+
SELECT 1;
7+
WARNING: cannot use the hint table
8+
HINT: Run "CREATE EXTENSION pg_hint_plan" to create the hint table.
9+
?column?
10+
----------
11+
1
12+
(1 row)
13+
14+
SET pg_hint_plan.enable_hint_table TO off;
15+
CREATE EXTENSION pg_hint_plan;
16+
SET pg_hint_plan.enable_hint_table TO on;
17+
SELECT 1;
18+
?column?
19+
----------
20+
1
21+
(1 row)
22+
23+
SET pg_hint_plan.enable_hint_table TO off;
24+
DROP EXTENSION pg_hint_plan;

pg_hint_plan.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "access/genam.h"
1414
#include "access/heapam.h"
1515
#include "access/relation.h"
16+
#include "catalog/namespace.h"
1617
#include "catalog/pg_collation.h"
1718
#include "catalog/pg_index.h"
1819
#include "catalog/pg_proc.h"
@@ -1891,6 +1892,25 @@ get_hints_from_table(const char *client_query, const char *client_application)
18911892
char nulls[2] = {' ', ' '};
18921893
text *qry;
18931894
text *app;
1895+
Oid namespaceId;
1896+
bool hints_table_found = false;
1897+
1898+
/*
1899+
* Make sure that hint_plan.hints is found before we attempt to look for
1900+
* a hint.
1901+
*/
1902+
namespaceId = LookupExplicitNamespace("hint_plan", true);
1903+
if (OidIsValid(namespaceId) &&
1904+
OidIsValid(get_relname_relid("hints", namespaceId)))
1905+
hints_table_found = true;
1906+
1907+
if (!hints_table_found)
1908+
{
1909+
ereport(WARNING,
1910+
(errmsg ("cannot use the hint table"),
1911+
errhint("Run \"CREATE EXTENSION pg_hint_plan\" to create the hint table.")));
1912+
return NULL;
1913+
}
18941914

18951915
PG_TRY();
18961916
{

sql/hint_table.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Tests for the hint table
2+
LOAD 'pg_hint_plan';
3+
4+
-- Attempting to use the hint table without the extension created
5+
-- emits a WARNING.
6+
SET pg_hint_plan.enable_hint_table TO on;
7+
SELECT 1;
8+
SET pg_hint_plan.enable_hint_table TO off;
9+
10+
CREATE EXTENSION pg_hint_plan;
11+
SET pg_hint_plan.enable_hint_table TO on;
12+
SELECT 1;
13+
SET pg_hint_plan.enable_hint_table TO off;
14+
DROP EXTENSION pg_hint_plan;

0 commit comments

Comments
 (0)