Skip to content

Commit 82884f5

Browse files
authored
SKIP_LOCKED in GP (#185)
Adds skip locked feature to GPDB. ANALYZE SKIPLOCKED will process all leaf partitions of tables, which are NOT currently locked. Note that this feature does not protect from concurrent lock acquisition, so semantics is "best-effort". Note that non-leaf partitions will not be updated even if no lock is actually held.
1 parent a95c938 commit 82884f5

File tree

11 files changed

+101
-18
lines changed

11 files changed

+101
-18
lines changed

src/backend/commands/analyze.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
680680
}
681681

682682
sample_needed = needs_sample(vacattrstats, attr_cnt);
683-
if (sample_needed)
683+
if (sample_needed && (!(vacstmt->options&VACOPT_NOWAIT) || (rel_part_status(RelationGetRelid(onerel)) == PART_STATUS_LEAF)))
684684
{
685685
rows = (HeapTuple *) palloc(targrows * sizeof(HeapTuple));
686686

@@ -694,7 +694,7 @@ do_analyze_rel(Relation onerel, VacuumStmt *vacstmt,
694694
if (inh)
695695
numrows = acquire_inherited_sample_rows(onerel, elevel,
696696
rows, targrows,
697-
&totalrows, &totaldeadrows);
697+
&totalrows, &totaldeadrows,vacstmt->options);
698698
else
699699
numrows = (*acquirefunc) (onerel, elevel,
700700
rows, targrows,
@@ -1823,7 +1823,7 @@ compare_rows(const void *a, const void *b)
18231823
int
18241824
acquire_inherited_sample_rows(Relation onerel, int elevel,
18251825
HeapTuple *rows, int targrows,
1826-
double *totalrows, double *totaldeadrows)
1826+
double *totalrows, double *totaldeadrows,int vacopts)
18271827
{
18281828
List *tableOIDs;
18291829
Relation *rels;
@@ -1838,8 +1838,9 @@ acquire_inherited_sample_rows(Relation onerel, int elevel,
18381838
* Find all members of inheritance set. We only need AccessShareLock on
18391839
* the children.
18401840
*/
1841+
LOCKMODE lock_mode = (vacopts & VACOPT_NOWAIT) ? NoLock : AccessShareLock;
18411842
tableOIDs =
1842-
find_all_inheritors(RelationGetRelid(onerel), AccessShareLock, NULL);
1843+
find_all_inheritors(RelationGetRelid(onerel), lock_mode, NULL);
18431844

18441845
/*
18451846
* Check that there's at least one descendant, else fail. This could

src/backend/commands/analyzefuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ gp_acquire_sample_rows(PG_FUNCTION_ARGS)
211211
num_sample_rows =
212212
acquire_inherited_sample_rows(onerel, DEBUG1,
213213
sample_rows, targrows,
214-
&totalrows, &totaldeadrows);
214+
&totalrows, &totaldeadrows,0);
215215
}
216216
else
217217
{

src/backend/parser/gram.y

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ static Node *makeIsNotDistinctFromNode(Node *expr, int position);
428428
%type <node> overlay_placing substr_from substr_for
429429

430430
%type <boolean> opt_instead
431-
%type <boolean> opt_unique opt_concurrently opt_verbose opt_full
431+
%type <boolean> opt_unique opt_concurrently opt_verbose opt_full opt_skip_locked
432432
%type <boolean> opt_freeze opt_default opt_ordered opt_recheck
433433
%type <boolean> opt_rootonly_all
434434
%type <boolean> opt_dxl
@@ -753,6 +753,8 @@ static Node *makeIsNotDistinctFromNode(Node *expr, int position);
753753

754754
YEZZEY
755755

756+
SKIP_LOCKED
757+
756758

757759
/*
758760
* The grammar thinks these are keywords, but they are not in the kwlist.h
@@ -1111,6 +1113,7 @@ static Node *makeIsNotDistinctFromNode(Node *expr, int position);
11111113
%nonassoc VERBOSE
11121114
%nonassoc UNKNOWN
11131115
%nonassoc ZONE
1116+
%nonassoc SKIP_LOCKED
11141117

11151118

11161119

@@ -11433,13 +11436,15 @@ vacuum_option_elem:
1143311436
;
1143411437

1143511438
AnalyzeStmt:
11436-
analyze_keyword opt_verbose opt_rootonly_all
11439+
analyze_keyword opt_verbose opt_skip_locked opt_rootonly_all
1143711440
{
1143811441
VacuumStmt *n = makeNode(VacuumStmt);
1143911442
n->options = VACOPT_ANALYZE;
1144011443
if ($2)
1144111444
n->options |= VACOPT_VERBOSE;
1144211445
if ($3)
11446+
n->options |= VACOPT_NOWAIT;
11447+
if ($4)
1144311448
n->options |= VACOPT_ROOTONLY;
1144411449
n->freeze_min_age = -1;
1144511450
n->freeze_table_age = -1;
@@ -11449,42 +11454,48 @@ AnalyzeStmt:
1144911454
n->va_cols = NIL;
1145011455
$$ = (Node *)n;
1145111456
}
11452-
| analyze_keyword opt_verbose qualified_name opt_name_list
11457+
| analyze_keyword opt_verbose opt_skip_locked qualified_name opt_name_list
1145311458
{
1145411459
VacuumStmt *n = makeNode(VacuumStmt);
1145511460
n->options = VACOPT_ANALYZE;
1145611461
if ($2)
1145711462
n->options |= VACOPT_VERBOSE;
11463+
if ($3)
11464+
n->options |= VACOPT_NOWAIT;
1145811465
n->freeze_min_age = -1;
1145911466
n->freeze_table_age = -1;
1146011467
n->multixact_freeze_min_age = -1;
1146111468
n->multixact_freeze_table_age = -1;
11462-
n->relation = $3;
11463-
n->va_cols = $4;
11469+
n->relation = $4;
11470+
n->va_cols = $5;
1146411471
$$ = (Node *)n;
1146511472
}
11466-
| analyze_keyword opt_verbose FULLSCAN qualified_name opt_name_list
11473+
| analyze_keyword opt_verbose opt_skip_locked FULLSCAN qualified_name opt_name_list
1146711474
{
1146811475
VacuumStmt *n = makeNode(VacuumStmt);
1146911476
n->options = VACOPT_ANALYZE;
1147011477
if ($2)
1147111478
n->options |= VACOPT_VERBOSE;
11479+
if ($3)
11480+
n->options |= VACOPT_NOWAIT;
1147211481
n->options |= VACOPT_FULLSCAN;
1147311482
n->freeze_min_age = -1;
11474-
n->relation = $4;
11475-
n->va_cols = $5;
11483+
n->relation = $5;
11484+
n->va_cols = $6;
1147611485
$$ = (Node *)n;
1147711486
}
11478-
| analyze_keyword opt_verbose ROOTPARTITION qualified_name opt_name_list
11487+
| analyze_keyword opt_verbose opt_skip_locked ROOTPARTITION qualified_name opt_name_list
1147911488
{
1148011489
VacuumStmt *n = makeNode(VacuumStmt);
1148111490
n->options = VACOPT_ANALYZE;
1148211491
if ($2)
1148311492
n->options |= VACOPT_VERBOSE;
11493+
if ($3)
11494+
n->options |= VACOPT_NOWAIT;
1148411495
n->options |= VACOPT_ROOTONLY;
1148511496
n->freeze_min_age = -1;
11486-
n->relation = $4;
11487-
n->va_cols = $5;
11497+
n->relation = $5;
11498+
n->va_cols = $6;
1148811499
$$ = (Node *)n;
1148911500
}
1149011501
;
@@ -11512,6 +11523,9 @@ opt_freeze: FREEZE { $$ = TRUE; }
1151211523
| /*EMPTY*/ { $$ = FALSE; }
1151311524
;
1151411525

11526+
opt_skip_locked: SKIP_LOCKED { $$ = TRUE; }
11527+
| /*EMPTY*/ { $$ = FALSE; }
11528+
;
1151511529
opt_name_list:
1151611530
'(' name_list ')' { $$ = $2; }
1151711531
| /*EMPTY*/ { $$ = NIL; }

src/include/commands/vacuum.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ extern int acquire_sample_rows(Relation onerel, int elevel,
230230
double *totalrows, double *totaldeadrows);
231231
extern int acquire_inherited_sample_rows(Relation onerel, int elevel,
232232
HeapTuple *rows, int targrows,
233-
double *totalrows, double *totaldeadrows);
233+
double *totalrows, double *totaldeadrows,int vacopts);
234234

235235
/* in commands/analyzefuncs.c */
236236
extern Datum gp_acquire_sample_rows(PG_FUNCTION_ARGS);

src/include/nodes/parsenodes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3109,7 +3109,7 @@ typedef enum VacuumOption
31093109
VACOPT_NOWAIT = 1 << 5, /* don't wait to get lock (autovacuum only) */
31103110
VACOPT_ROOTONLY = 1 << 6, /* only ANALYZE root partition tables */
31113111
VACOPT_FULLSCAN = 1 << 7, /* ANALYZE using full table scan */
3112-
VACOPT_YEZZEY = 1 << 30 /* YEZZEY VACUUM */
3112+
VACOPT_YEZZEY = 1 << 30 /* YEZZEY VACUUM */
31133113
} VacuumOption;
31143114

31153115
typedef enum AOVacuumPhase

src/include/parser/kwlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ PG_KEYWORD("show", SHOW, UNRESERVED_KEYWORD)
407407
PG_KEYWORD("shrink", SHRINK, UNRESERVED_KEYWORD) /* GPDB */
408408
PG_KEYWORD("similar", SIMILAR, TYPE_FUNC_NAME_KEYWORD)
409409
PG_KEYWORD("simple", SIMPLE, UNRESERVED_KEYWORD)
410+
PG_KEYWORD("skip_locked",SKIP_LOCKED, RESERVED_KEYWORD) /* GPDB */
410411
PG_KEYWORD("smallint", SMALLINT, COL_NAME_KEYWORD)
411412
PG_KEYWORD("snapshot", SNAPSHOT, UNRESERVED_KEYWORD)
412413
PG_KEYWORD("some", SOME, RESERVED_KEYWORD)

src/test/isolation2/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ install: all gpdiff.pl gpstringsubs.pl
7272
installcheck: install installcheck-parallel-retrieve-cursor installcheck-ic-tcp installcheck-ic-proxy
7373
./pg_isolation2_regress $(EXTRA_REGRESS_OPTS) --init-file=$(top_builddir)/src/test/regress/init_file --init-file=./init_file_isolation2 --psqldir='$(PSQLDIR)' --inputdir=$(srcdir) --schedule=$(srcdir)/isolation2_schedule
7474

75+
76+
installcheck-mdb: install
77+
./pg_isolation2_regress $(EXTRA_REGRESS_OPTS) --init-file=$(top_builddir)/src/test/regress/init_file --init-file=./init_file_isolation2 --psqldir='$(PSQLDIR)' --inputdir=$(srcdir) --schedule=$(srcdir)/isolation2_schedule_mdb
78+
79+
7580
installcheck-resgroup: install
7681
./pg_isolation2_regress $(EXTRA_REGRESS_OPTS) --init-file=$(top_builddir)/src/test/regress/init_file --init-file=./init_file_resgroup --psqldir='$(PSQLDIR)' --inputdir=$(srcdir) --dbname=isolation2resgrouptest --schedule=$(srcdir)/isolation2_resgroup_schedule
7782

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- Scenario to test SKIP_LOCKED usage
2+
3+
1: create table tp(id integer) DISTRIBUTED by (id) PARTITION by RANGE (id) (START (1) END (1000) EVERY (100), DEFAULT PARTITION extra);
4+
CREATE
5+
1: insert into tp (select generate_series(1,1000));
6+
INSERT 1000
7+
1: begin;
8+
BEGIN
9+
1: lock table tp_1_prt_3;
10+
LOCK
11+
2: analyze SKIP_LOCKED tp;
12+
ANALYZE
13+
1: rollback;
14+
ROLLBACK
15+
16+
17+
18+
1: create table t1(id integer) DISTRIBUTED by (id) PARTITION by RANGE (id) (START (1) END (1000) EVERY (100), DEFAULT PARTITION extra);
19+
CREATE
20+
1: insert into tp (select generate_series(1,400));
21+
INSERT 400
22+
1: begin;
23+
BEGIN
24+
1: lock table tp_1_prt_3;
25+
LOCK
26+
2: analyze SKIP_LOCKED tp;
27+
ANALYZE
28+
1: rollback;
29+
ROLLBACK
30+
31+
1:drop table tp;
32+
DROP
33+
1:drop table tp1;

src/test/isolation2/isolation2_schedule

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,5 @@ test: copy_insert_utility_mode_hierarchies
343343
test: free_remapper_fatal
344344

345345
test: spilling_hashagg
346+
347+
test: skip_locked_vacuum_analyze
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: skip_locked_vacuum_analyze

0 commit comments

Comments
 (0)