The Aditi Deductive Database System
The Aditi Deductive Database System
1. Introduction
Deductive databases are logic programming systems designed for applications with
large quantities of data, the kinds of applications that are at present programmed
in SQL embedded in a host language such as C. Deductive databases generalize
relational databases by exploiting the expressive power of (potentially recursive)
logical rules and of non-atomic data structures, greatly simplifying the task of
application programmers.
The ultimate goal of the Aditi project at the University of Melbourne is to show
that this generalization does not have to compromise performance. Applications
that can run on relational systems should run on deductive systems with similar
performance while being substantially easier to develop, debug, and maintain. We
Jayen Vaghani, BSc (hons), is Systems Programmer; Kotagiri Ramamohanarao, Ph.D., is Professor; David
B. Kemp, Ph.D., is Postdoctoral Research Fellow; Zoltan Somogyi, Ph.D., is Lecturer; Peter J. Stuckey,
Ph.D., is Senior Lecturer;, Tim S. Leask, BSc (hons), is Systems Programmer; and James Harland, Ph.D.,
is Lecturer; Collaborative Information TechnologyResearch Institute, Department of Computer Science,
University of Melbourne and RMIT, Parkville, 3052 Victoria, Australia.
246
server supporting multiple clients. In Aditi, each client has one server process
dedicated to it, but it also shares some server processes with other clients. Figure
1 illustrates how the pieces fit together. The responsibilities of the three types of
server processes are as follows:
DAP Each Database Access Process (DAP) is dedicated to a client process. DAPs
are responsible for authenticating their clients to the rest of Aditi, and for
controlling the evaluation of the queries of their clients.
RAP Relational Algebra Processes (RAPs) carry out relational algebra operations
on behalf of the DAPs. RAPs are allocated to DAPs for the duration of one
such operation.
QS The Query Server (QS) is responsible for managing the load placed by Aditi
on the host machine by controlling the number of RAPs active at any time. In
operational environments, there should be one QS per machine. However, in
a development environment one can set up multiple QSs; each QS supervises
an entirely separate instance of Aditi.
When a client wants service from Aditi, it sends a command to its DAE The
DAP executes some types of commands by itself and passes the other types (mostly
relational algebra operations) to the QS. The QS queues the task until a RAP
becomes available and then passes the task on to a free RAP for execution. At
completion, the RAP sends the result to the requesting DAP and informs the QS
that it is available for another task.
Aditi base relations have a fixed arity, but attributes currently do not have associated
types; any ground term can appear in any attribute position of any relation. Each
relation has a sequence of one or more attributes as its key. If requested, Aditi
will ensure that a relation has at most one tuple with a given combination of values
for the key attributes.
The creator of a base relation can currently choose between four storage
structures. The simplest storage structure does no indexing. The data are stored as
an unsorted sequence of tuples. Although obviously not suited for large relations,
this storage structure has very low overheads and J[s quite appropriate for small,
static or mostly-static relations.
The second storage structure has B-tree indexing. The data are stored as a
sequence of tuples, and a B-tree sorted on the key attributes of the relation serves
as an index into this sequence; the sequence can be sorted if the user requests.
The B-tree index is of a variable-bucket type. The relation's creator specifies two
numbers N and B, and the node size can then vary from NB bytes to (2N-1)B
bytes. The two objectives of this somewhat unconventional design are high space
utilization and low overhead for relations that are created once and then used
more or less unchanged several times, a usage pattern characteristic of temporary
relations in deductive databases and shared by some permanent relations as well.
Tuples in the data file are initially all contiguous; holes appear only when a
tuple is deleted. Aditi keeps track of these holes in a freelist, and newly inserted
tuples are put into holes whenever possible. The data file of temporary relations
will thus always have 100% space utilization. As for the index, the variable bucket
size adjusts the amount of storage allocated to a node to the amount of storage
required by the node, resulting in space utilization factors in the 90-95% range. The
space efficiency of our B-tree structure reduces I/O costs for some usage patterns
important to deductive databases (full scans of small-to-medium sized relations). For
access patterns where clustering is important, a conventional B-tree or a multikey
hashed file (see below) would be more effective.
During the creation of a relation, our B-tree structure reduces overhead by
never requiring tuples in the data file to be moved; the data stay where they are even
when the index node pointing to them is split. There is an overhead associated with
the management of the bucket size, but our tests have shown it to be minimal. In
any case, the relation creator can avoid this overhead (at the expense of worsening
space utilization) by setting N equal to 1 and thus requesting fixed-size buckets.
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 249
The third storage structure has a superimposed codeword index. The data are
stored as a sequence of tuples where each tuple fits entirely into one segment. The
index has a bit string for each segment. This bit string is the segment descriptor,
and it is calculated by ORing together the tuple descriptors of all the tuples in
the segment. Tuple descriptors in turn are derived by hashing the values of the
attributes of the tuple and using each hash value to select a fixed number of bits
to be set.
When the same technique is used to generate a descriptor for a partial-match
query on the relation (i.e., one that specifies values for some attributes but not
others), it is only necessary to set the bits corresponding to the values of the specified
attributes. Therefore the query descriptor will have fewer Is than tuple descriptors.
The key observations are that a tuple cannot match the query unless the tuple's
descriptor has Is in all positions where the query descriptor has ls, and that a
segment cannot contain a matching tuple unless the segment's descriptor has ls in
all positions where the query descriptor has ls.
The first step in the retrieval process is therefore to apply this test to all the
segment descriptors. Because the test looks at only the bits that are set in the
query descriptor, and does not look at all the bits of segment descriptors, the index
stores the segment descriptors in a bit-slice fashion, with bit 0 of all descriptors
first, bit 1 of all descriptors next, and so on. This clusters together all the descriptor
bits needed in the test and reduces both I/O and computation costs. However, the
contiguous placement of the bit slices also places an upper bound on the number
of segments of the relation. Since segments have no overflow pages in this storage
method, this implies a static maximum size for the relation. This is why we call this
storage structure the static superimposed codeword index (ssimc).
The ssimc removes the need to retrieve pages that do not contain tuples matching
the query, but these tuples themselves can be scattered throughout the data file. To
improve clustering, and thus I/O performance and memory consumption, whenever
Aditi inserts a tuple into an ssimc relation, it interleaves some bits from the hash
values of the tuple's attributes (which it must compute anyway) to form a segment
number, and inserts the tuple into the indicated segment whenever possible. If that
segment is full, Aditi tries to find another segment with sufficient space. If none
exists, the insertion fails.
The fourth storage structure, the dynamic superimposed codeword index (dsimc),
is specifically designed for dynamic relations, in fact for relations whose size may
fluctuate by several orders of magnitude. It is very similar to ssimc, but there are two
key differences. First, dsimc stores all the bits of a descriptor together, removing the
limit on the size of relations. Second, whereas ssimc relies mainly on superimposed
codewords and uses multikey hashing as a tuple-placement heuristic, dsimc relies
mainly on multikey linear hashing and uses superimposed codewords as a filtering
heuristic. In fact, one can think of dsimc as multikey hashing; before retrieving a
data page, the system checks the page's descriptor to see whether the page contains
any potentially matching tuples. Much of the time the answer will be "no," and in
250
such cases we can avoid reading that data page and any associated overflow pages.
The cost we pay, reading the descriptor, is small because the descriptor is small,
and because it is clustered with other descriptors that the same query is likely to
need.
Both the ssimc and dsimc structures require relation creators to specify some
information from which they compute the parameters of the superimposed coding
and multikey hashing algorithms. For a fuller explanation of research on techniques
for deriving the optimal values of these parameters, see Ramamohanarao and
Shepherd (1986 for ssimc; 1990 for dsimc); these papers have the full details of the
two storage structures as well. As we will show in Section 4.4, Aditi's design allows
other storage structures to be added later.
The creation of a relation begins with the creation of a schema (unless an
appropriate schema already exists). The following; command creates a schema
named f l i g h t d i s t _ s c h e m a of arity 3, the first two arguments forming a key, and
with the flexible tuple structure (Section 4.5).
17. newschema flightdist_schema 3 flex 1,2
This command then creates the relation f l i g h t d i s t with this schema, using B-tree
indexing with fixed 1 kilobyte nodes and with no duplicates allowed:
27. newrel flightdist flightdist_schema btree 1 1024 0
One can then insert facts into this relation via the command
3X newtups flightdist 3
sydney, honolulu, 8165
honolulu, toronto, 7464
So far we have talked only about base relations. However, the power of deductive
databases lies in derived relations, relations that can infer information at run-
time. In Aditi, users define derived relations (also called derived predicates) by
writing programs in Aditi-Prolog, a pure (declarative) variant of Prolog augmented
with declarations. Some declarations tell the compiler about the properties of the
predicate (e.g., which arguments will be known when the predicate is called); others
request specific rule transformations or evaluation strategies. The file stops, a l is
an example:
?- mode(stops(f,f,f)).
?- f l a g ( s t o p s , 3, d i l l ) .
flight(Origin, Destination).
stops(Origin, Destination, [StoplStoplist]) "-
f l i g h t ( O r i g i n , Stop),
stops(Stop, Destination, S t o p l i s t ) ,
not i n _ l i s t ( S t o p , S t o p l i s t ) .
?- m o d e ( i n _ l i s t ( f , b ) ) .
?- f l a g ( i n _ l i s t , 2, magic).
?- f l a g ( i n l i s t , 2, d i l l ) .
i n _ l i s t ( H e a d , [Headl_Tail]).
i n _ l i s t ( I t e m , [HeadlTail]) "-
i n _ l i s t ( I t e m , Tail).
This code fragment defines two predicates (derived relations): stops gives all
possible routes between all city-pairs, and represents routes as lists of intermediate
stops; i n _ l i s t checks whether its first argument is a member of the list that is its
second argument.
The lines beginning with ?- are declarations to the compiler (declarations
in Prolog-like languages traditionally look like queries because they also cause
immediate action by the compiler). The first mode declaration states that stops
supports queries such as ?- stops(From, To, S t o p l i s t ) in which all arguments
are free variables. The second mode declaration states that i n _ l i s t supports queries
such as ?- i n _ l i s t ( C i t y , [sydney, honolulu]) in which the first argument is
a free variable but the second argument is bound to a completely specified (i.e.,
ground) term.
In general, every relation must have one or more mode declarations; base
relations always have one mode consisting of all f's. A query matches a mode
declaration if it has ground terms in all positions that the mode declares to be
b (bound); the query may have any term in positions the mode declares to be f
(free). Aditi allows a query on a relation only if it matches one of the modes of
that relation. For example, since there is no "f,f" mode for i n _ l i s t , Aditi will
reject the query ?- i n _ l i s t ( C i t y , S t o p l i s t ) .
There are two reasons for requiring mode declarations:
1. Some predicates have an infinite number of solutions unless certain arguments
are specified in the call.
2. Most optimization methods require that the optimizer know which arguments
of each call are known when the call is processed.
It is the flag declarations that tell the compiler what optimizations and evaluation
algorithms to use. With the flags shown above, Aditi will use differential (semi-
naive) evaluation when answering queries on both relations; for i n _ l i s t , it will
use the magic set optimization as well. (Actually, the diff flags can be omitted, as
differential evaluation is the default.)
252
The set of flags supported by the current version of Aditi allows users to request
• naive evaluation
• differential or semi-naive evaluation (Balbin and Ramamohanarao, 1987)
• predicate semi-naive evaluation (Ramakrishnan et al., 1990)
• evaluation by magic set interpreter (Port et al., 1990)
• the magic set transformation (Bancilhon et al., 1986; Beeri and Ramakrishnan,
1987)
• the supplementary magic set transformation (Sacca and Zaniolo, 1987)
• the context transformations for linear rules (]Kemp et al., 1990)
• parallel evaluation (Leask et al., 1991)
• top-down tuple-at-a-time evaluation (Section 4.1)
This is quite a long list. What's more, users may specify several flags for
one predicate. For example, one can request differential evaluation of a magic
set transformed program, as i n _ l i s t above does, or one can ask for a context-
transformed program to be evaluated using a parallel implementation of the predicate
semi-naive algorithm. However, not all combinations of flags make sense (e.g., the
various magic set variants and the context transformation are all mutually exclusive).
The flexibility offered by the flag mechanism in selecting the set of optimizations
to be applied to a predicate is essential to us as researchers when we investigate
optimization methods and evaluation algorithms. Eventually, of course, we intend to
distill the results of these investigations into code that automatically selects the best
set of optimizations for each predicate in the program (as in the strategy module
of Morris et al., 1987).
' Aditi-Prolog programs may include disjunction and negation. Like most de-
ductive databases, Aditi currently supports only stratified forms of negation. In the
future this may change, since we have developed a practical algorithm for computing
answers to queries even in the presence of unstratified negation (Kemp et al., 1991,
1992). This algorithm works on a magic-transformed program where each predicate
has two slightly different versions. At each iteration, the algorithm uses one set of
versions to compute a set of definitely true facts, the other set to compute a set of
possibly true facts. The next iteration can then use the complement of the set of
possibly true facts as the set of definitely false facts it needs to perform negation.
Aditi-Prolog supports the usual set of aggregate operations. Given an atomic
query, one can ask for the minimum or maximum 'value of some attribute in the
solution, the sum of the values of some attributes, the number of solutions, or a
list of those solutions. In addition, one can ask for the solution to the query to
be divided into groups based on the values of some attributes before the aggregate
operation is carried out. For instance, the query
will find the length of the longest flight in the database and print the origin,
destination, and length of the few flights (probably one) that have this distance. On
the other hand, the query
?- aggregate(Max = max(Dist), [From], flightdist(From, _To, Dist)),
flightdist(From, To, Max).
will print the details of at least one flight from each city, because the groupby list
tells Aditi to divide the solutions of the flightdist relation into groups, one group
per starting point, and then find the longest flight(s) in each group.
When aggregation is part of an Aditi-Prolog program, it must respect stratification
as well as negation. For further details of the language, see the Aditi-Prolog language
manual (Harland et al., 1992a).
The Aditi-Prolog compiler (ape) interface is intentionally similar to the interface
of other compilers on Unix systems (e.g., cc). As arguments, one just names the
files to be compiled:
4Z apc s t o p s . a l
The resulting object file, stops .ro can be tested by loading it manually into the
query shell (Section 3.1). When the programmer is satisfied that the predicates in
the file work correctly, he or she can make them a part of the database by executing
the command
5~ newderived s t o p s . r o
All three kinds of interfaces actually send to the DAP queries expressed in a very
low level language called RL. Section 3.5 describes this language, while Section 3.6
describes the compiler that converts programs and queries from Aditi-Prolog to RL.
terminal interfaces to Prolog interpreters. Its main loop prints a prompt, waits
for the user to type in an Aditi-Prolog query, and evaluates the query. A simple
example:
The main difference from traditional Prolog interpreters is that the query shell
prints all answers to the query at once. The query shell accepts any Aditi-Prolog
query, including those with disjunction, negation, aggregation, or any combination
of these. To answer a query, the query shell transforuas the query into a rule whose
body is the query itself and whose head is an atom with a new predicate symbol
whose arguments are the variables the user is interested in: these are either the
variables before the colon (if there is one) or all the variables in the query. The
query shell then uses the Aditi compiler (Section 3.6) to translate the query into
RL, the language of the DAI~ and passes the translated query to the DAP to be
executed. When the operation is complete, the DAP leaves its result in a temporary
relation; the query shell looks up this relation and displays its contents to the user.
In the usual course of events, the temporary relation is then deleted. However,
the user can request that the temporary relation be retained until the end of the
current session by naming it, via a query such as
2 <- yankee_airlines(Airline) := hq(Airline, new_york).
This creates a new temporary relation yankee_airlines that can be accessed by
later queries in this session. The new relation has arity 1 and its contents is the list
of the airlines whose headquarters are in New York. Note that this query does not
set up a rule; the content of y a n k e e _ a i r l i n e s is not updated if the hq relation is
modified.
Updates to permanent base relations are accomplished similarly, by prefacing
a query with an atom referring to a permanent base relation and a + = sign (for
insertions) or a - = sign (for deletions). For example, the following query deletes
all flights that either start or finish at snowed-in airports:
The code of the query shell is an adaptation of the main loop of the NU-Prolog
interpreter (which itself is written in NU-Prolog). The query shell's ability to record
VLDB Journal 3 (2) Vaghani:The Aditi Deductive DB System 255
a history of previous queries, and to recall a given previous query, possibly edit it,
and then execute it, derives from this source. For a full list of the capabilities of
the query shell, see Harland et al. (1992b).
Aditi includes a prototype of a query shell that accepts SQL as input. This prototype
is based on an SQL to NU-Prolog translator that was developed several years ago
by Philip Dart at the University of Melbourne as an educational tool for our
undergraduates. The translator implements most of the SQL standard, the main
exceptions being null values and (for the time being) updates.
SQL queries do not make distinctions between base and derived relations. The
query
will elicit the same response from the SQL query shell as the query
5 <- trip(From, To, Trip), not flight(From, To).
will from the Aditi-Prolog query shell. Both ask Aditi to "list all trips in the database
that are not direct flights."
The main problem with the SQL query shell is that SQL requires relations
to be in first normal form, and relations in Aditi may violate this assumption by
containing or computing non-atomic attribute values. Therefore, the SQL query
shell can access only some Aditi relations (those in 1NF) without being confused.
All these actions can be performed from a command line as well. Xqsh is useful
because its prompts obviate the need to remember the order and type of what
would otherwise be command line arguments, and because it supports browsing.
The only significant functionality xqsh lacks is the ability to start and stop the query
server. Since xqsh cannot function without a running query server, this functionality
is available from a Unix shell command line only.
Xqsh is easily extensible and therefore specializable to the input and output
requirements of particular applications. We have built a variant called xflight for
our flights database (Harland and Ramamohanarao, 1992). This variant presents
users with a form listing the various parameters of a trip to be planned (where from,
where to, departure and arrival date and time limits, airline preferences, etc.), and
asks them to fill in some or all of these parameters. When the users signal that
they have filled in all the fields they want, xflight assembles the Aditi-Prolog query,
executes it, interprets the results, and displays them on a map of the world.
find_flights :-
get_input(Origin, Dest, Date),
trip(Origin, Dest, Date, Trip),
cheap_enough(Trip),
pretty_print(Trip).
The predicate get_input prompts the user for the origin and destination of a trip
and the desired date of travel. The call to t r i p finds all trips (sequences of flights
with an airline, flight number, and cost for each, with short stopovers, say less than
four hours between them) between the two named h)cations starting on the named
date. The call to cheap_enough acts as a filter, rejecting all trips above a certain
price, while p r e t t y _ p r i n t prints the details of the trips that pass the cheap_enough
filter.
The idea behind Aditi's NU-Prolog interface is the same as the idea behind
SQL embedded in C or Cobol: Use a database language for database tasks and
use a host language to take care of procedural tasks such as I/O. Therefore, in
this example g e t _ i n p u t , cheap_enough, and p r e t t y _ p r i n t will be NU-Prolog
predicates, and t r i p will be an Aditi predicate.
VLDB Journal 3 (2) Vaghani:The Aditi Deductive DB System 257
find_flights :-
get_input(Origin, Dest, Date),
dbQuery(trip(Origin, Dest, Date, Trip), Answers),
process(Answers).
After the call to dbQuery, the variable Answers contains a pointer to a table
containing the solutions of t r i p ( 0 r i g i n , DeBt, Date, Trip). The program may
access the elements of this table by the call readTuples(Answers, Trip); the
interface predicate readTuples will successively instantiate Trip to one of the
answers to the Aditi query. Thus, the problem of finding the cheapest flights, the
shortest flights, and the flights on a given airline reduces to performing three searches
of the table. These searches rely on cursors like those found in SQL environments;
these cursors can be implicit (as above) or explicit. However, NU-Prolog is so
close to Aditi-Prolog that many pieces of code are accepted by compilers for both
languages, and so we expect very little need for the use of explicit cursors; they are
provided more for completeness than anything else.
258
For more details on the Aditi/NU-Prolog interface, including the facilities for
updating Aditi relations from NU-Prolog, see Harland et al. (1992a).
?- flag(path, 2, context).
?- mode(path(b,f)).
path(X, Y) :- edge(X, Y).
path(X, Y) :- •dg•(X, Z), path(Z, Y).
This code declares a derived predicate called path that expects to be called with its
first argument ground, and directs the compiler to apply the optimization method
known as the context transformation (Kemp et al., 1990). The body of the predicate
defines path to be the transitive closure of the edge relation, just as the t r i p
predicate is a kind of transitive closure of the f l i g h t relation. We have chosen
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 259
path as the example because the additional details in the code produced for g r i p
would needlessly clutter the explanation.
Given the four-line definition of path at the beginning of this section, the
Aditi-Prolog compiler produces the following RL code. The only modification wc
have made for presentation was to add some white space.
procedure
path_2_l(init_path_2_l, output_path_2_l)
relation init_path_2_l, output_path_2_l;
{
relation diff_mc_path_2_l_2_l, edge_4, ~inal_mc_path_2_l_2_l;
relation out_eq_l, out_eq_7, edge_9, output_mc_path_2_l_2_l;
bool booll;
int sizel;
setbrel(final_mc_path_2_l_2_l, 2);
settrel(diff_mc_path_2_l_2_l, 2);
settrel(edge_4, 2);
settrel(out_eq_l, 2);
settrel(out_eq_7, 2);
settrel(edge_9, 2);
settrel(output_mc_path_2_l_2_l, 2);
project(init_path_2_l,out_eq_l,'C#(O,O),#(O,O)");
btmerge(final_mc_path_2_l_2_l,out_eq_l,diff_mc_path_2_l_2_l,O);
setprel(edge_4, "edge'', 2);
labell:
join(diff_mc_path_2_l_2_l,edge_4,"#(1,0)=#(O,l)",''",
out_eq_7,"#(O,O),#(l,l)");
btmerge(final_mc_path_2_l_R_l,out_eq_7,diff_mc_path_2_l_2_l,O);
cardinality(diff_mc_path_2_l_2_l, sizel);
gt(sizel, O, booll);
test(booll, labell);
flatten(final_mc_path_2_l_2_l,output_mc_path_2_l_2_l);
setprel(edge_9, C'edge'', 2);
join(output_mc_path_2_l_2_l,edge_9,''#(1,0)=#(O,1)","'',
output_path_2_l,''#(O,O),#(1,1)'');
clear(diff_mc_path_2_l_2_l);
clear(edge_4);
clear(final_mc_path_2_l_2_l);
clear(out_eq_l);
clear(out_eq_7);
clear(edge_9);
clear(output_mc_path_2_l_2_l);
260
Note the (intentional) similarity to the "look and feel" of a C program. The name
of the RL procedure is derived from the name of the Aditi-Prolog predicate it
implements (path), its arity (2), and the number of the mode it implements (1).
The names of some of the local variables have two copies of this suffix; one comes
from the arity and mode of path, the other from the arity and mode of the auxiliary
predicate me_path_2_ 1 created by the compiler as part of the context transformation
on path_2_1 (the "me" stands for "magic context"). The RL code above is in fact
compiled from code that looks like this:
?- mode(mc_path_2_l(b,f)).
mc_path_2_l(C, C).
mc_path_2_l(C, A) :- mc path_2_l(C, B), edge(B, A).
?- mode(path(b,f) ).
path(X, Y) :-mc_path_2_l(X, A), edge(A, Y).
This code exists only inside the compiler. Since the auxiliary predicate cannot be
called from anywhere else, its RL code has been inlined into the RL code of path
itself.
By convention, all RL procedures generated by the compiler have two arguments.
The first is always a relation whose tuples represent the values of the input or bound
arguments of the predicate it implements; the second is always a relation whose
tuples represent the values of all the arguments of the predicate it implements.
In this case, i n i t _ p a t h _ 2 _ l has one attribute while output_path_2_l has two,
because path has two arguments, only one of which is input in mode number 1.
When path_2_1 is called, the i n i t _ p a t h _ 2 _ l relation must already be known,
but path_2_1 is responsible for determining the contents of output_path_2_1 (in
fact any tuples in output_path_2_l at the time that path_2_1 is called will be
overwritten and discarded).
The body of the procedure path_2_ 1 begins with the declaration of some local
variables, and continues with the creation of empty relations, all of arity 2, to serve
as the initial values of the relation-valued variables. The first of these initializations
calls the procedure s e t b r e l to give final_mc_path_2_l_2_l a B-tree index whose
key is the hash value of the entire tuple; the other temporary relations have no
index. The call to setprel puts into edge_4 a pointer to the permanent base relation
called "edge" of arity 2.
The arguments of many RL operations consist of the names of input relations
and output relations, selection conditions, and projection specifications. Selection
conditions and projection specifications contain occurrences of strings of the form
"# (n,m)": these refer to attribute m of input relation n, where both attributes and
input relations are numbered from zero.
The project operation therefore puts into out eq_ 1 one tuple for each tuple in
init_path_2_l, and both attributes of this tuple are taken from the only attribute
of init_path_2_l. The btmerge operation then takes the out_eq_l relation,
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 261
inserts its tuples into final_mc_path_2_l_2_l, and puts any tuples that were
not already in final_mc_path_2_l 2_1 into the relation cliff me_path 2 1_2_1.
Since final_mc_path_2_l 2 1 was initially empty, this will copy out_eq 1 into
both final_me_path 2_1_2_1 and diff_mc_path_2_l_2_l. This implements the
first rule of me_path_2_ 1.
The loop invariant being established is that, at the beginning of the Nth iteration,
the second argument of final_mc_path_2_l 2_1 always holds the places reachable
from the first argument by traversing N or fewer edges, and dill_me_path_2_ 12 1
always holds the tuples that were inserted into final_mc_path_2_l 2_1 in the N-lth
iteration (the initialization counts as iteration 0).
The loop starts with the label "label1": its body implements the second rule
of me_path_2 1 with differential evaluation. The join takes the mc path 2_1 facts
generated in the previous iteration and joins them to the edge relation as dictated
by the conjunction mc_path_2_l(C, B), edge(B, A). The join condition states
that #(1,0) and #(0,1), the first argument of the second input relation and the
second argument of the first input relation, respectively, must be equal, since both
correspond to the variable B. The result of the join is a relation with three attributes,
representing the variables C, B, and A. Since the head of the rule contains only
C and A, the result is projected onto C and A (#(0,0) and #(1,1), respectively)
before it is assigned to relation out_eq_7 which thus contains tuples corresponding
to the me_path_2 1 facts we have discovered in this iteration. The empty string
after the join condition is the argument of the join operation that would construct
new values during the join itself (e.g., by performing arithmetic on attributes of
the join result) for use in the projection specifications; this is not needed in this
example (Section 4.3).
The btmerge operation scans the out eq_7 relation, looking up each tuple in
the f i n a l mc path_2_ 1_2_ 1 relation, and using the indexing on that relation. If the
tuple already exists in f i n a l mc path_2 1 2_1, it does nothing more; otherwise it
inserts the tuple into both final_mc_path_2_l_2_l and diff_mc_path_2_l_2_l.
This maintains the invariant and completes the processing of the second rule of
mc_path_2_l.
The next three lines find out whether the loop should continue. The first puts
the cardinality of the difference relation into the integer variable s i z e l . The second
sets b o o l l to true or false, depending on whether s i z e l is greater than zero. If it
is, then the iteration just finished has discovered new mc path 2_1 facts and thus
we must go back to the start of the loop at l a b e l l to try to use these new facts
to generate even more facts; otherwise, we exit the loop.
The remaining code is executed only after loop exit: it implements the
only rule of the transformed path predicate. The flatten operation copies
the B-tree relation final_mc_path_2_l_2_l into the flat (non-indexed) relation
output mc_path_2_l_2_l. Then the join operation implements the conjunction
mc_path_2_l (X, A), edge (A, Y), the join condition requiring the equality of the
two ~s, and the project specification putting the values of X and Y into the relation
262
appropriate for different modes of the predicate involved, so the compiler creates
sips separately for each mode of the predicate. Every rule needs its own sip. The
chief component in the creation of this sip is the ordering of the literals in the
bodies of clauses. The compiler does this by a kind of insertion sort. From all
the remaining uninserted literals in the rules, by default the compiler chooses the
leftmost literal that has a valid mode given the variables bound by the already
inserted literals. This selection rule can be changed using the flags maxboundsips
and min:~reesips, which cause the compiler to select the literal with the most bound
arguments and the literal with the fewest free arguments, respectively. These are
both reasonable heuristics for reducing the size of intermediate relations.
Users who require precise control over the chosen sip can use the default
left-to-right sip strategy together with specialized versions of rules for each mode.
Consider the definition
? - f l a g ( p a t h , 2, c o n t e x t ) .
?- mode (path (b, f) ).
.7- mode ( p a t h ( f , b ) ) .
path(X, Y) :- edge(X, Y).
path(X, Y) :-mode(b,f) :: edge(X, Z), path(Z, Y).
path(X, Y) :-mode(f,b) :: path(Z, Y), edge(X, Z).
Sections 4.1 through 4.3 describe Database Access Processes, Query Servers, and
Relational Algebra Processes, respectively. Sections 4.4 and 4.5 describe the common
code base on which all three types of processes are built.
Users must go through DAPs to gain access to Aditi; the DAPs have the responsibility
of enforcing the security restrictions (if any) on access to shared databases. DAPs
are trusted processes; they always check whether the user has the authority to carry
out the operation he or she requests the DAP to perform. A front-end process
(such as the query shell) can get access to Aditi in one of two ways. It can create a
child DAP process and set up a pipe to it, or it can have a copy of the DAP built in.
Though the latter method is faster, for obvious reasons it is confined to application
programs that are themselves trusted. An example of a trusted application is the
NU-Prolog interpreter. Because a NU-Prolog program cannot compromise the
internal data structures of the NU-Prolog interpreter, all NU-Prolog programs are
trusted as well (Figure 1).
After startup and the completion of the obligatory privilege check, DAPs go
into a loop, waiting for commands and executing them. These commands are not
in Aditi-Prolog but in a binary form of RL; DAPs contain an interpreter for this
266
language. Each DAP has a table containing the names and the codes of the RL
procedures it knows. At startup, this table will be empty, but by looking up the data
dictionary, the DAP can on demand retrieve the RL procedures of the database's
derived relations.
To execute a query on a single predicate whose arguments are all distinct free
variables, the front-end process tells the DAP to execute the corresponding RL
procedure in its procedure table if the predicate being accessed is a derived relation;
otherwise it tells the DAP to execute the built-in setprel command with the name
of the base relation as an argument. To execute any query more complex than this,
the front-end process must compile the query into an RL procedure, load that RL
procedure into the DA_, and tell the DAP to execute that procedure. The DAP will
leave the results of queries in temporary relations; the front-end can then ask the
DAP for the contents of these relations and may use them in later queries (Section
3.1). If the result of the query is to be used as a set of tuples to be inserted into or
deleted from a relation, the front-end will command the DAP to do that as well.
An RL procedure consists of a sequence of operations. The DAP interpreter
executes some operations itself and sends others to the QS for assignment to some
RAE Operations that can require large amounts of computation, such as joins
and unions, are performed by RAPs; operations with small time bounds, such as
determining a relation's cardinality, creating a relation, or inserting a tuple into a
relation, are performed directly by the DAE
DAPs also execute one other kind of operation: those that are simply not suited
for Aditi's usual evaluation algorithm. For example, when Aditi evaluates predicates
for list manipulation such as list reverse and append in a bottom-up set-at-a-time
manner, each iteration of the RL code involves several relational algebra operations,
several context switches, and several buffer cache accesses just to add or remove one
element at the front of a list. To avoid all this overhead, we have made top-down
tuple-at-a-time computation available to Aditi users by embedding a NU-Prolog
interpreter in each DAE
When the compiler sees a predicate flagged as a top-down predicate in the
Aditi-Prolog source file, it copies the predicate to a NU-Prolog source file and
compiles it using the NU-Prolog interpreter. Calls to this predicate are compiled
into an RL instruction that invokes this NU-Prolog object code in the DAP's NU-
Prolog interpreter. Note the predicate must be acceptable to both compilers by
which it is processed, Aditi-Prolog and NU-Prolog--this is not difficult due to the
dose relationship between the two languages.
Apart from top-down predicates, most predicates spend most of their time in
the RAPs. Normally, whenever the DAP sends a job to a RAP through the QS, it
will wait for the RAP to report the completion of the job. However, the compiler
can generate code in which sending a job to a RAP and waiting for a completion
report are two separate actions, in between which the DAP can continue running
and perform work in parallel with a RAP. Because the DAP can continue to issue
other jobs to other RAPs, several RAPs can work for the same DAP at the same
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 267
time.
The two main types of parallelism in Aditi-Prolog programs are parallelism
between atoms inside a clause, and parallelism between clauses. Parallelism between
atoms in a clause is possible only if neither atom needs a value generated by the other.
Parallelism between clauses is restricted by the nature of bottom-up evaluation, which
must apply nonrecursive clauses before it goes into a loop applying the recursive
clauses. Therefore, parallelism is possible only among the nonrecursive clauses and
among the recursive clauses, not between a nonrecursive clause and a recursive
clause. Many predicates have only one recursive clause. However, if this clause is
non-linear (i.e., it has two or more recursive calls) then differential evaluation and
the magic set transformation will both effectively split this clause into two or more
clauses that can be evaluated in parallel.
The DAP executes every RL operation in the context of a thread. Sequential RL
code only has one thread. Parallel code creates and destroys threads as necessary.
We have developed two compilation schemes for managing thread activity (Leask
et al., 1991). One is based on the fork-join approach and the other on dataflow
methods. The fork-join approach is very simple: whenever the compiler knows
that two or more operations or operation sequences are independent, it creates a
thread for each operation sequence. The main thread launches all these threads
simultaneously and then waits for all of them to complete before continuing.
In theory, the compiler will generate one thread for each nonrecursive clause,
with each of those threads launching separate threads for separate atoms, and it will
generate a similar thread structure inside the loop. In practice, the thread structure
will be less complicated because independent atoms inside clauses are somewhat
rare, and because many important predicates have only one non-recursive clause
and/or only one recursive clause. However, as long as the predicate has s o m e
independent operations, the fork-join scheme will exploit it.
The weakness of the fork-join approach is its handling of loops. It cannot start
the next iteration of a loop until the threads of all recursive clauses have completed,
even if the threads that have completed so far have produced the relations needed
by some of the threads to start their part of the next iteration. This leads to a loss
of concurrency. Our other compilation scheme therefore controls synchronization
between threads by dataflow methods: A thread can execute its next operation if
the operations that produce its input relations have completed.
Our experiments have proven that the dataflow method can yield greater concur-
rency and better performance than fork-join without increasing run-time overhead,
but only if a set of mutually recursive predicates have some recursive rules inde-
pendent of each other, which is quite rare. The cost is increased complexity in the
RL interpreter and in the Aditi-Prolog compiler. In fact, while the RL interpreter
in the DAP supports both schemes (it had to for us to run our experiments), the
compiler currently generates only fork-join code.
Note-that neither scheme requires the DAP itself to be parallel. All of the
parallelism comes from the parallel operation of several RAPs. The DAP does
268
maintain several thread contexts, but it executes in only one context at a time; it
switches to another thread only when its current thread exits or blocks waiting for
a reply from a RAE Since the operations native to the DAP are all much shorter
than a typical RAP operation, this is a very effective simulation of time-slicing.
The Query Server (QS) is the central management process of Aditi; it must be
running for Aditi to be available to users. At startup, it is responsible for initializing
the entire database system according to a specified global configuration file and
starting the appropriate number of RAPs. During normal operation, its main task
is to keep track of the state of each RAP and to allocate tasks to free RAPs. The
QS listens on its message queue waiting for various requests and either performs
them immediately or queues them until they can be performed. Requests can come
from DAPs and RAPs. A DAP can make requests to login to Aditi, to have a task
executed, to get the status of the system, and to logout from Aditi. It can also send
the QS a message to shut down the system if its user is authorized to do so. A
RAP can tell the QS that it has finished an assigned task and is ready for another
task, or it can inform the QS that it is aborting due to some fatal error.
By controlling the number of running RAPs and DAPs and keeping usage
statistics for each user, the QS can perform load management for Aditi. Controlling
the number of RAPs and DAPs allows the QS to prevent Aditi from overloading the
host system. Recording usage statistics means that the QS can prevent individual
users from overloading Aditi itself.
The QS can control the number of DAPs by refusing to login new DAPs when
the load on the system is too high. Since DAPs place relatively small loads on the
system, such action should be quite rare. Control over the number of RAPs is more
critical; relational algebra operations are the most expensive operations performed
in a database system and can easily overload the host machine. The QS prevents
this by not passing DAP requests onto the RAPs when the load on the machine is
dangerously high, and by not creating more RAPs than the machine can support.
This way many DAPs can be running, providing service to many users, but the
overall number of jobs being performed is strictly controlled.
Relational Algebra Processes are the workhorses of Aditi; they execute the most
expensive operations in RL. They are all relational algebra operations, either standard
ones (e.g., join, union, difference, select, project) or combinations such as union-diff
and btmerge (Section 3.5). RAPs spend their lifetime waiting for a job from the
QS, executing the job, and sending the result to the requesting DAP while notifying
the QS of their availability. A RAP exits only when the QS commands it to shut
down or it encounters a fatal error.
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 269
The message to a RAP specifies the name of the operation to be performed, the
input relations, select conditions for these input relations, any data specific to the
operation (e.g., join attributes), and a list of output relations along with projection
information that specifies how each tuple in those relations is to be constructed
from the result of the operation. All relational algebra operations can therefore
be preceded by select operations and followed by project operations. The select
conditions on the input relations often allow thee, RAP to confine its attention to
the relevant parts of the input relations without incurring the overhead of creating
temporary relations to hold those parts. In the same spirit, the projection information
allows the RAP to allocate storage only for the part of the output that is actually
required; the fact that more than one output relation can be specified (presumably
each with a different projection) allows the RAP to avoid the overhead of scanning
the result of the operation several times. A secondary benefit of pre-select and
post-project is a reduction of the communication overhead between the DAP and
the RAP (this is also true for combination operations like btmerge).
Depending on the size and structure of the argument relations, the RAP can
choose from several algorithms to carry out its various operations. The operation
whose performance is most important is the join. Since no single join method works
optimally in all situations, Aditi currently supports four join algorithms: sort/merge
join, hash join, nested block join, and indexed join.
Sort~mergejoin, like other sort-based Aditi operations, uses the external merge-
sort algorithm described by Knuth (1973). To make sure that the overhead of
extracting sort keys from a tuple is paid only once, rather than every time the tuple
appears in a comparison, our implementation uses prepended sort keys (Linderman,
1984). Since the schema format supports relations with prepended keys, our sort
routine will leave the sort keys in place if the calling procedure indicates it wishes
to use them. Thus, relations can have their keys extracted and be sorted just once
instead of once per operation; such presorting can substantially reduce query time.
Our sort routine can write its results to a temporary relation or pass them
back on demand to the calling RAP operation. A RAP executing a sort/merge
join will sort each input relation on the attributes being joined, and then scan
the results looking for matching tuple pairs. The sort routine will help the join
algorithm in two ways. First, it removes duplicate tuples as soon as it discovers
them. Duplicate elimination has proved essential for recursive queries because it
restricts the exponential growth of the sizes of temporary files. Each iteration of a
bottom-up computation can double the number of tuple copies in a relation (and
more than double the time required to process the relation if it outgrows memory).
Second, the merge-sort routine performs the final merge pass on demand just as
the RAP requests the next tuple in its result; the final merge passes on the two
input relations are interleaved with the scanning pass of the join algorithm itself.
This arrangement reduces the number of passes over the data and the attendant
I/O overhead. However, the sort/merge code still starts with a check to see whether
either relation is already sorted on the join attributes; correctly sorted files are used
270
as they are.
The sort/merge join sorts to bring tuples with the same join attributes together.
The hash join algorithrn does this clustering by splitting each input relation into
partitions based on the hash value of the join attributes of each tuple. It then joins
pairs of partitions with the same hash values in a nested loop fashion.
The hash join and the sort/join methods are applicable in the same situations.
The hash join will generally perform better, exceptions mostly caused by its greater
sensitivity to data skew and its inability to always remove duplicates from the input
relations. This is because not all of a partition will be in memory when a tuple is
inserted into that partition, and hence the tuple cannot be compared against all
tuples in that partition.
Although we are considering various heuristics that will allow us to choose
dynamically between sort/merge join and hash join for joining large relations,
currently the decision is made statically in the query server's configuration file.
The configuration file also gives the size below which a relation is considered small:
if at least one input relation to a join is small, then the RAP uses one of its other
join methods.
The first of these methods, nested block join, requires that one of the relations
be small enough to fit into memory. It loads the tuples from the small relation into
a hash table in memory, and then scans the tuples in the large relation sequentially.
As the scan reaches a tuple, the algorithm hashes its join attributes and looks in the
hash table for tuples to join with this tuple. Because this algorithm examines the
data in each relation only once, it is about four times faster than either sort/merge
join or hash join. This speedup factor means that splitting up the small relation
into memory-sized chunks and using nested block join on each chunk is worthwhile
whenever the small relation is less than four times the buffer size. It is the size of
this memory buffer that is governed by the configuration file; we avoid computing
this size automatically to allow us to switch easily between the various join methods
for performance testing.
The last join method supported by Aditi is inde~:edjoin. For each tuple in the
small relation, the algorithm performs an indexed lookup on the other relation to
retrieve all the tuples that have matching attribute values, and then joins them with
the tuple from the small relation. This is the preferred method when one of the
input relations only has a small number of tuples, and the other input relation has an
index on the attributes required for the join. We do not use it when both relations
are large because this would require a large number of lookups with bad locality.
In such cases, the better locality of sort-merge join y/elds better performance, even
though it accesses more tuples (unless the ratio between the size of the larger
relation and the threshold is greater than the ratio of I/O costs of indexed lookups
and sequential accesses).
Currently, only permanent relations can be indexed on attributes and so only
joins with permanent relations will use this method. (The sort key of B-tree indexed
temporary files is the hash value of the entire tuple, since this is needed for duplicate
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 271
elimination.) Joins involving only temporary relations will use only the other join
methods. This is not too much of a problem because temporary relations tend to
be relatively small, and joins involving large relations are very likely to have at least
one input that is a permanent relation. Our tests show quite dearly the importance
of indexing permanent relations to allow the use of this join method. Appropriate
indexing accelerated some of our tests by factors of up to 100 (Section 5).
Currently, each relational algebra instruction is carried out on a single ~ but
there is no fundamental reason that multiple RAPs cannot cooperate on a single
task. We are working on schemes to enable multiple RAPs to co-operate in sorting
a single relation in parallel, and on parallel hash based join algorithms (Nakayama
et al., 1988; Tham, 1993).
4.4.1 The Indexing Layer. The database library provides routines for creating and
deleting temporary and permanent relations; inserting, deleting, and retrieving tuples
from relations; and tuple-level and relation-level locking. The task of the top layer
in the database library, the interface layer, is to hide the differences between the
implementations of these operations by the various indexing methods supported by
Aditi. The database interface layer therefore acts as a switch; for the most common
operations, the real work is done by code specific to an indexing method. This code
forms the indexing layer, the layer below the interface layer.
4.4.2 The System Interface Layer. The lowest layer of Aditi is the system interface
layer, which provides the rest of Aditi with a set of low-level services including
message passing, locking, and disk buffering and caching. The two reasons for
the existence of the system interface layer are: our desire for OS independence
in the rest of Aditi to make porting easier; and the need to bypass inefficient OS
mechanisms whenever possible.
The current version of the system interface layer runs on most Unix implemen-
tations that support the IPC facilities of System V, in particular System V shared
memory. Some type of shared memory is essential for good performance, since it
allows Aditi's server processes to communicate among themselves without incurring
the overhead of system calls; they use the message-passing routines supplied by
the system interface layer instead. These routines pass messages in shared memory
272
Application[
be writing to a given temporary relation and that no other processes will access
this relation while it is being written. However, several processes may read a single
temporary relation simultaneously; this can occur if parallel query processing is
being used.
The problem with this scheme is that if any process other than the creator needs
access to the temporary relation, that process cannot read it from the creator's private
cache. Instead, the creator must flush the relation out of its cache onto disk, where
the other process can see it. For a short-lived temporary relation, the benefits of
reduced contention may be squandered in the extra disk traffic. Profiling tests have
shown that the creating and unlinking of the disk file for a temporary relation can
often be the most significant part of the overall CPU and I/O costs associated with
a temporary relation. Much of this cost is due to the pathname-to-inode translation
that takes place within the kernel whenever a file is created or unlinked.
To alleviate these problems, Aditi implements what we term "in-core" temporary
relations. Aditi stores the names of in-core relations in a table in shared memory,
and allocates pages to such relations from the shared buffer cache. Once allocated,
such pages remain locked in memory: they are never written to disk and are only
released when the in-core relation is deleted. Due to size limits on shared memory,
there are limits on the number of in-core relations that can exist at any one time;
on the number of pages that an in-core relation can have in memory; and on the
total number of buffer cache pages that can hold data from in-core relations (we
typically set the last limit at about 30% of the cache size). Pages in excess of these
limits are stored on disk. The in-core relation table keeps track of the number of
such overflow pages, as well as the name of the file in which they are stored and
their location.
This arrangement solves both problems: pages in the shared buffer cache do
not have to be flushed to disk to become visible to other processes, and disk I/O is
avoided for small and short-lived temporary relations by delaying disk I/O and even
the creation of the disk file until overflow actually occurs. Since most temporary
relations are small and short-lived, this is an important optimization. Our data show
that in-core temporary relations typically speed up query processing by a factor of
four, the main benefits coming from the avoidance of disk file creates and unlinks.
The query processor therefore creates all temporary relations in-core by default. If
ever a temporary file cannot be created in-core due to the maximum in-core file
limit being reached, the query processor will create it as a normal disk file.
In-core temporary relations are implemented at the lowest level as a storage
type. The higher levels need only specify a flag at create time to say whether a
temporary relation should be in-core or not. Thus we can create unindexed files,
btree files, ssimc files, and dsimc files in-core with no change to any of the indexing
code.
At present, Aditi has no transaction mechanism. We are currently modifying the
code of the buffer caches to support the ARIES model of write-ahead logging (Mohan
et al., 1992). We are also looking at higher level issues related to transactions (e.g.,
274
how best to resolve simultaneous updates). As recurshze computations can take long
periods of time, overlapping updates are more likely in deductive databases than
in relational databases. We do not want to lock relations for long periods because
this can drastically reduce concurrency. However, optimistic methods that force
restarts of computations could cause starvation and waste computation resources.
We would prefer a hybrid solution that minimizes these problems.
4.4.3 Sacuri~. Aditi has a very simple security system that provides minimal pro-
tection of a user's data. The security system recognizes only two levels of privilege:
Aditi superusers (DBAs) and other registered Aditi users. The system controls
who can read, modify, or delete a relation. When a relation is created, only the
creator has access to it. The creator can then modify' the relation's permissions in
the following four ways:
• The creator can allow all users to read the data in the relation.
• The creator can allow all users to insert data into, delete data from, or modify
data in the relation. This implies the ability to read the relation.
• The creator can allow DBAs (Aditi superusers) to delete the relation.
• The creator can allow any user (including DBAs) to delete the relation.
The creator of a relation always remains the only user who can change privileges
on a relation. Any of the above privileges can be revoked at any time. Revoking
read privilege automatically revokes write privilege, and revoking superuser delete
privilege automatically revokes public delete privilege.
Currently, Aditi has no way of restricting the set of users who can create relations
in a database. As long as a user has permission to attach to an Aditi server, that
user can create a relation in any database visible from that server. This, of course,
will change as Aditi matures.
. to ] . . . I s o. 8 2.3
Sbits24 bits ~ _ ~ * functortemplxte~ ~"
The first two tuple types are only in experimental use now, since Aditi currently
stores RL code in Unix files and it does not yet have a type system that can impose
maximum sizes on attributes. Therefore, all Aditi applications so far use the third
tuple type, which we call "flexible." This structure allows arbitrary ground Prolog
terms (e.g., lists with nested function symbols) to be stored directly in a relation
and then manipulated by code written in RL. RL operations can project subterms
and construct new terms as well as test the values of terms and subterms.
To support fast access to individual attributes, the flexible tuple format has a
fixed sized header for each attribute, and it stores these headers contiguously. This
header is a 32 bit word, of which the top 8 bits are for type information and the
remaining 24 bits are either for the data, if they fit, or for an offset pointing to
where the data are stored in the tuple (since this offset is a byte offset, tuples can
be up to 16 Mb in size). Small signed and unsigned integers, very short strings, and
atoms with very short names fit directly in the header; large integers, floating point
numbers, long strings, atoms with long names, and all structured terms do not.
For example, consider the tuple < a , sydney, foo(2.3)> whose storage map
appears in Figure 3. When creating this tuple, Aditi first allocates space for 3 32-bit
headers. Since the atom "a" fits into 24 bits, it tags the first attribute as a "small
atom" and inserts "a" in its data field. Since the atom "sydney" does not fit into
24 bits, Aditi pads it out to the nearest word size, in this case to eight bytes, and
puts it at the current end of the tuple. Aditi then tags the second attribute as a
plain "atom," computes the distance from the start of the second attribute position
(at offset 4) to where the actual value is stored (at offset 12) and inserts 8 as the
relative offset in the data field.
We chose the attribute position rather than the start of the tuple as the base
for the offset to allow attributes to be passed around without needing to know
from which tuple they came. The padding is needed to ensure that all headers are
always word aligned, which in turn is necessary on most machines to allow numeric
values such as integers to be used directly, without having to first move them to a
word-aligned position.
When Aditi inserts the third attribute, which is a functor with sub-terms, into
the tuple, it tags the third header as a functor and stores a relative offset of 12 in
the data area (the tuple is now 20 bytes long, and the third attribute itself starts
at offset 8). It then constructs a functor template at the current end of the tuple.
The functor template consists of a functor header followed by an array of attribute
276
headers, one for each of the subterms of the functor.. In this case, the functor has
arity 1, so there is only one attribute header.
The functor header is a 32 bit quantity with the top 8 bits containing the arity
and the bottom 24 bits containing an offset to the functor name, which is stored
directly after the functor template. (Since the length of the template is implicit in
the arity, this offset is redundant, but its presence saves us from having to Calculate
it each time.) In this example, the relative offset to the functor name is 8: "foo"
is stored after the functor template padded out to four bytes.
Aditi inserts a subterm into a functor template exactly as it performs the
insertion of an attribute into a tuple. This is possible because arrays of headers
for the subterms of a functor look exactly the same as arrays of headers for the
attributes of a tuple. Therefore the insertion of the value "2.3" tags foo's only
attribute header as a "floating point number" and puts the bits representing the FP
number 2.3 at the current end of the tuple and stores the relative offset (8) in the
data field of the attribute header.
Overall, the tuple is 36 bytes in length, but the tuple library does not know this
until the tuple has been completely constructed. Accordingly, the tuple building
routines always initially allocate a small amount of memory, and check how much
space is left as they insert new values. If they run out, they allocate a piece of
memory twice as large as before, copy the tuple to the new space and then insert
the value. This copying is needed to keep tuples contiguous, but it can become
expensive, especially if the tuple exceeds its current space allowance several times.
To avoid this repeated copying, layers above the Aditi tuple library can set the size
of the initial space allocation or even supply their own space if they know how large
the tuple will be.
5. Performance
In this section we report on two sets of experiments..All tests were performed on a
Silicon Graphics 4D/340S running IRIX 4.0.1 with four 33 MHz R3000 processors
and 64 megabytes of memory. The tests were carried out in multiuser mode with
other users present and active, but with the system load average below 2 most of the
time. The test database was stored on a striped filesystem, track-interleaved across
two SGI-supplied disk drives connected via two separate 4.0 Mb/s synchronous
SCSI-1 interfaces. The drives have an average seek time of 11.9 ms, a single-track
seek time of 2.5 ms, an average rotational latency of 6.25 ms (4800 rpm), and a
maximum transfer rate of 2.3 Mb/s. The times we report are elapsed real times in
seconds computed as the average of four or more hans. We report elapsed times
instead of CPU times because they show how the system would appear to a user
(CPU time measurements leave out context switches, disk I/O, and other similar
effects).
The first set of experiments used the path predicate of Section 3.5 to compare
the context transformed and magic set transformed programs against the original
VLDB Journal 3 (2) Vaghani:The Aditi Deductive DB System 277
Large query
size 8 i0 12 14 16 18
orig 5.5: 1.0 30.6: 1.0 257.6: 1.0 3058.0: 1.0
magic 5.6: 1.0 17.8: 1.7 56.8: 4.5 168.3: 18.2 453.0: NA 1160.0: NA
context 4.4: 1.3 8.6: 3.5 24.0: 10.7 52.1: 58.7 107.8: NA 209.2: NA
program. Our test query was ?- t (X), path (X, Y) with the base relation t supplying
input values for the call to path.
Our experiments varied the contents of the relations edge and t. The edge
relation contained full binary trees of various sizes (i.e., tuples of the form edge(i,
2/) and edge(i, 2/+1) for values of i ranging from 1 up to a maximum that depends
on the size of the tree. Our six data sets used maximums of 255, 1023, 4095,
16383, 65535, and 262143, corresponding to tree depths of 8, 10, 12, 14, 16, and
18, respectively. Each version of the edge relation had a B-tree index on its first
attribute.
We used two versions of the t relation. The first contained only the tuple
"100." We label the results achieved with this version "small query." The other
version contained 100 tuples randomly chosen from the middle three levels of the
full binary tree of the relevant edge relation; we label the results achieved with this
version "large query." We chose only trees of even depths for edge because the
middle three levels move downward one level only when the total depth of the tree
increases by two.
Table 1 reports our results. Rows correspond to optimization flags on the path
relation, while columns correspond to the depths of the edge relation. Speedups
are computed with respect to the untransformed program; they follow the times
they refer to after a colon. One can make several observations based on the data
in the table.
The number of tuples in the t relation makes virtually no difference for the
untransformed program because it computes the entire path relation and then joins
it with the t relation.
The magic set-transformed program always performs better than the untrans-
formed program, and the context set-transformed program always performs better
than the magic transformed program. The magic/original performance ratio climbs
very rapidly as the size of the edge relation increases. The context/magic perfor-
mance ratio also climbs but not as rapidly, reaching a maximum of 9.5 to 1 at depth
18 for the small query and a maximum of 5.5 to 1 at depth 18 for the large query.
This illustrates that the context transformation performs relatively better when the
278
?- aditi(trip(b,b,b,b,b,b,f,f,f,f,f,f)).
?- flag(trip, 12, magic).
The four queries all involve a hypothetical traveller called Phileas Fogg who
wants to travel around the world, spending one week in each of several regions.
The four queries differ in the constraints imposed on the tour.
• Tour 1 must visit Asia, Europe, North America, and the Pacific region.
• Tour 2 must visit Asia, Europe, and North America.
• Tour 3 must visit Europe, North America, and the Pacific region.
• Tour 4 must visit Europe and North America.
The tours must visit the named regions in the order in which they are given; all
tours start and finish in Melbourne, Australia. The queries all follow the same
pattern; the structure of the query for tour 4 is:
the cost of the joins invoked by Zrip depends mostly on the sizes of the input
relations and very little on the size of the output relation.
As one expects, accessing such a large relation without an index has a large
penalty, ranging from about 17- to 24-fold. For these queries the trip predicate
always specifies all three of the key arguments of the schedule relation, so B-
tree indexing is as effective as it can be. The dsimc indexing yields slightly lower
performance (by about 10% to 20%), mainly because dsimc uses the keys only to
restrict its attention to a set of pages and cannot focus directly on the tuples of
interest within those pages.
For the weekly schedule, in which the relation contains 1,044 tuples, most of
the time is spent in computation, not retrieval, and so the type of indexing makes
little difference: there is less than 10% variation among all the numbers. The main
sources of this variation are probably the differences between the overheads of the
various indexing methods.
To show the effect of queries that specify values only for some of the attributes
in the key, we tested two simple queries on the daily schedule relation. The first
specified the origin and the date, the second the destination and the date. The
results appear in Table 3.
Table 3 shows the limitations of B-tree indexing. If the query does not specify
a key attribute, B-tree indexing cannot make use of any attributes following the
unspecified one in the key list. In this case, the key list on the schedule relation
was "origin, destination, date." The first query achieves moderate performance
because it uses only the first key attribute; the second query results in very bad
performance because it cannot use any of the key attributes (this performance is
worse than unindexed due to overheads). On the other hand, the multi-attribute
hashing scheme on which the dsimc method is built is not at all sensitive to the
order of keys, so it can exploit two key attributes for both queries. The difference
between the performance of the two dsimc queries can be explained by the different
number of answers they have in our database (11 for the From Melbourne query
and 7 for the To Melbourne query); the two queries take about the same time per
answer.
The daily schedule data occupy about 4.5 Mb of data in ASCII format. After
the data are inserted into a Btree-indexed relation, they occupy about 7.4 Mb with
about another 4.9 Mb required for the index. Similarly, the weekly schedule data
occupy 95 Kb in ASCII; in Aditi it takes 164 Kb for the data file and 106 Kb
for the index. We used the daily schedule relation, which contains 54,058 tuples,
282
as test data in measuring the rate at which tuples can be inserted into relations.
The insertion rates into relations without indexing, ,with B-tree indexing and with
dynamic superimposed codeword indexing were roughly 790 tuples per sec.ond, 420
tuples per second, and 60 tuples per second, respectively. The lower speed of dsimc
insertion is due to repeated copying of some tuples as the underlying linear hashed
file doubles in size several times. This overhead can be avoided if the code knows
in advance the number and average size of the tuples to be inserted.
Based on our experience with other tests, we can state that the effectiveness
of many optimizations is strongly influenced by the number of iterations required
to complete the bottom-up computation. Sometimes, a technique will introduce a
one-off overhead that cannot be compensated for by the reduced cost per iteration
because there are not enough iterations in the bottom-up computation. Presorting
of relations is one such technique, although it can double performance on some
queries. At other times, an optimization technique may speed up each iteration but
require more iterations to answer a query. Unfortunately, one cannot always decide
in advance whether a given optimization is worthwhile; the answer is often dependent
on the query and the data. More experimentation is required to determine whether
any useful heuristics exist. We envision the compiler exploiting such heuristics by
generating code that switches at run-time, depending on the characteristics of the
data, between two or more ways of evaluating a query.
being implemented. It supports complex terms containing function symbols but not
variables. Aditi is unique among the five systems in being able to exploit parallelism.
It is also unique in its ability to mix bottom-up set-at-a-time evaluation with top-down
tuple-at-a-time computation.
7. Conclusion
Aditi is a practical deductive database system. The lower layers are based on
relational technology wherever possible but with ne.cessary modifications such as
support for tuples not in first normal form. The upper layers are derived mostly
from research specific to deductive databases. Many aspects in both layers build
on original research by members of the Aditi team (Balbin and Ramamohanarao,
1987; Ramamohanarao and Shepherd, 1986; Ramamohanarao et al., 1988; Kemp
et al., 1989, 1990, 1991, 1992; Balbin et al., 1991; I-Iarland and Ramamohanarao,
1993; Kemp and Stuckey, 1993). Much of this research in turn used Aditi as a
testbed in which to try out and evaluate ideas.
Aditi is a true database system. It is disk based, it is multiuser, and it can
exploit parallelism in the underlying machine. It has both textual and graphical user
interfaces, including an interface that understands SC!L. It also includes interfaces to
the NU-Prolog language, to which Aditi-Prolog is closely related. NU-Prolog code
can call Aditi-Prolog; this link allows Aditi applications to create their own user
interface. Aditi-Prolog code also can call NU-Prolog to compute some predicates
top-down and tuple-at-a-time when such evaluation speeds up query processing.
Our projects for the future include:
• Integrating our prototype compiler, which generates code for non-stratified
negation into the main Aditi system.
• Implementing high-level optimizations such as constraint propagation (Kemp
et al., 1989) and the new transformation for linear predicates (Harland and
Ramarnohanarao, 1993).
• Implementing low-level optimizations such as redundant code elimination.
• Extending Aditi-Prolog by providing types and a sophisticated system of
modes based on these types.
• Using the mode system to allow the handling of partially-instantiated data
structures. We already know how to do this without subsumption tests
(Somogyi et al., 1994).
• Adding object-oriented and higher-order features to Aditi-Prolog; we already
have a draft language proposal.
• Finishing the implementation of the transaction system.
• Benchmarking Aditi against RDBMSs such as Oracle and Ingres on both
relational and deductive type queries.
VLDB Journal 3 (2) Vaghani: The Aditi Deductive DB System 285
Those who would like to learn more about Aditi should refer to the Aditi users's
guide (Harland et al., 1992a), the Aditi-Prolog language manual (Harland et al.,
1992b), and the report describing the flights database from which the second set of
tests (Section 5) was drawn (Harland and Ramamohanarao, 1992).
The Aditi system itself is available to interested researchers. It has already been
distributed to about a dozen sites around the world.
Acknowledgments
Many people contributed significantly to Aditi, both by performing research and
by developing software. We would like to acknowledge the contributions of Kim
Marriott, John Shepherd, David Keegel, Warwick Harvey, Philip Dart, and Jeff
Schultz in this respect. We thank Gill Dobbie for her comments on a draft of this
article.
This research was supported by grants from the Australian Research Coun-
cil through the Machine Intelligence Project, from the Victorian Department of
Manufacturing and Industry Development, through the Collaborative Information
Technology Research Institute, from the Australian Department of Industry, Tech-
nology and Commerce through the Key Centre for Knowledge-based Systems, and
from the Australian Cooperative Research Center program through the Center for
Intelligent Decision Systems.
References
Balbin, I., Port, G., Ramamohanarao, K., and Meenakshi, K. Efficient bottom-up
computation of queries on stratified databases. Journal of Logic Programming~
11(3/4):295-345, 1991.
Balbin, I. and Ramarnohanarao, K. A generalization of the differential approach
to recursive query evaluation. Journal of Logic Programming 4(3):259-262, 1987.
Bancilhon, E, Maier, D., Sagiv, Y., and Ullman, J. Magic sets and other strange ways
to implement logic programs. Proceedingsof the Fifth Symposium on Principles of
Database Systems, Washington, DC, 1986.
Beeri, C. and Ramakrishnan, R. On the power of magic. Proceedingsof the Sixth
ACM Symposium on Principles of Database Systems, San Diego, CA, 1987.
Bocca, J. Megalog: A platform for developing knowledge base management sys-
tems. Proceedingsof the Second International Symposium on Database Systems for
Advanced Applications, Tokyo, 1991.
Carey, M., DeWitt, D., Richardson, J., and Shekita, E. Object and file management in
the EXODUS extensible database system. Proceedingsof the TwelfthInternational
Conference on l,~tyLarge Databases, Kyoto, Japan, 1986.
286
Chimenti, D., O'Hare, T., Krishnamurthy, R., Naqvi, S., Tsur, S., West, C., and
Zaniolo, C. An overview of the LDL system. IEEEDataEngineerin~ 10(4):52-62,
1987.
Freeston, M. Grid files for efficient Prolog clause access. In: Gray, E and Lucas,
R., eds., Prolog and Databases, Chicester, England: Ellis Horwood, 1988, pp.
188--211.
Harland, J., Kemp, D.B., Leask, T.S., Ramamohanarao, K., Shepherd, J.A., Somogyi,
Z., Stuckey, P.J., and Vaghani, J. Aditi-Prolog language manual. Technical Report
92/27, Department of Computer Science, University of Melbourne, Australia,
November 1992a.
Harland, J., Kemp, D.B., Leask, T.S., Ramamohanarao, K., Shepherd, J.A., Somogyi,
Z., Stuekey, P.J., and Vaghani, J. Aditi user's guide. Technical Report 92/26,
Department of Computer Science, University of Melbourne, Australia, November
1992b.
Harland, J. and Ramamohanarao, K. Experiences with a frights database. Techni-
cal Report 92/28, Department of Computer Science, University of Melbourne,
Australia, 1992.
Harland, J. and Ramamohanarao, K. Constraint propagation for linear recursive
rules. Proceedings of the Tenth International Con]~rence on Logic Programming
Budapest, Hungary, 1993.
Kemp, D.B. and Stuckey, P. Analysis-based constraint query optimization. Proceed-
ings of the the Tenth International Conference on Logic Programming Budapest,
Hungary, 1993.
Kemp, D.B., Ramamohanarao, K., Balbin, I., and Meenakshi, K. Propagating con-
straints in recursive deductive databases. Proceedings of the First NorthAmerican
Conference on Logic Programming Cleveland, OH, 1989.
Kemp, D.B., Ramamohanarao, IC, and Somogyi, Z. Right-, left-, and multi-linear
rule transformations that maintain context information. Proceedings of the Six-
teenth International Conference on ~ty Large Data Bases, Brisbane, Australia, 1990.
Kemp, D.B., Stuckey, EJ., and Srivastava, D. Magic sets and bottom-up evaluation
of well-founded models. Proceedings of the 1991 International Logic Programming
Symposium, San Diego, CA, 1991.
Kemp, D.B., Stuckey, EJ., and Srivastava, D. Query restricted bottom-up evaluation
of normal logic programs. Proceedings of the Joint International Conference and
Symposium on Logic Programming Washington, DC, 1992.
Knuth, D.E. Sorting and searching. Vol. 3, Chapter 5.4.1, The Art of Computer
Programming Reading, MA: Addison-Wesley, 1973.
Leask, T.S., Ramamohanarao, K., and Stuckey, EJ. Exploiting parallelism in bottom-
up computation in Aditi. Proceedings of the ILPS Workshop on Deductive Data-
bases, San Diego, CA, 1991.
Linderman, J. Theory and practice in the construction of a working sort routine.
Bell Laboratories Technical Journal, 63(8(part 2)):1827-1843, 1984.
VLDB Journal 3 (2) Vaghani:The Aditi DeductiveDB System 287
Mohan, C., Hadede, D., Lindsay, B., Pirahesh, H., and Schwarz, E ARIES: A trans-
action recovery method supporting fine-granularity locking and partial rollbacks
using write-ahead logging. ACM Transactions on Database Systems, 17(1):94-162,
1992.
Morris, K., Naughton, J.F., Saraiya, Y., Ullman, J.D., and Gelder, A.V. YAWN!
(yet another window on NAIL). IEEE Data Engineering~ 10(4):28-43, 1987.
Morris, K., Ullman, J.D., and Gelder, A.V. Design overview of the NAIL! system.
Proceedings of the Third International Conference on Logic Programming~ London,
1986.
Mumick, I.S., Finkelstein, S.J., Pirahesh, H., and Ramakrishnan, R. Magic condi-
tions. Proceedings of the Ninth Symposium on the Principles of Database Systems,
Nashville, TN, 1990.
Nakayama, M., Kitsuregawa, M., and Takagi, M. Hash-partitioned join method using
dynamic destaging strategy. Proceedings of the Fourteenth Conference on l~ry Large
Databases, Los Angeles, 1988.
Naqvi, S. and Tsur, S. A Logical Language for Data and Knowledge Bases. New York:
Computer Science Press, 1989.
Ousterhout, J.K. Tcl and the Tk Toolkit. Reading, MA: Addison-Wesley Publishers,
1994.
Phipps, G., Derr, M., and Ross, K. Glue-Nail: A deductive database system. Pro-
ceedings of the Tenth ACM Symposium on Principles of Database Systems, Denver,
CO, 1991.
Port, G., Balbin, I., and Ramamohanarao, K. A new approach to supplementary
magic optimisation. Proceedings of the First Far-East Workshop on Future Database
Systems, Melbourne, Australia, 1990.
Ramakrishnan, R., Srivastava, D., and Sudarshan, S. Rule ordering in bottom-up
ffixpoint evaluation of logic programs. Proceedings of the Sixteenth International
Conference on l~ry Large Databases, Brisbane, Australia, 1990.
Ramakrishnan, R., Srivastava, D., and Sudarshan, S. CORAL: A deductive database
programming language. Proceedings of the Eighteenth International Conference on
l,~ryLarge Databases, Vancouver, Canada, 1992.
Ramamohanarao, K. and Shepherd, J. A superimposed codeword indexing scheme
for very large Prolog databases. Proceedings of the Third International Conference
on Logic Programming~ London, 1986.
Ramamohanarao, K. and Shepherd, J. Partial match retrieval for dynamic files
using superimposed codeword indexing. In: Balaguruswamy, E. and Sushila,
B., eds., Computer Systems and Applications: Recent Trends. New Dehli, India:
McGraw-Hill, 1990.
Ramamohanarao, IC, Shepherd, J., Balbin, I., Port, G., Naish, L., Thorn, J., Zobel,
J., and Dart, E The NU-Prolog deductive database system. In: Gray, E and
Lucas, R., eds. Prolog and Databases, Chicester, England: Ellis Horwood, 1988,
pp. 212-250.
288
Sacca, D. and Zaniolo, C. The generalized counting method for recursive logic
queries. Proceedings of the International Conference on Database Theo~ Rome,
1986.
Sacca, D. and Zaniolo, C. Implementation of recursive queries for a data language
based on pure horn logic. Proceedings of the Fourth International Conference on
Logic Programming Melbourne, Australia, 1987.
Somogyi, Z., Kemp, D.B., Harland, J., and Ramamo]~anarao, K. Subsumption-free
bottom-up evaluation of logic programs with partially instantiated data struc-
tures. Proceedings of the Fourth International Conference on Extending Database
Technolog)¢ Cambridge, England, 1994.
Tham, J. Duplicate removal and parallel join algorithms. Technical Report/Honours
Report, Department of Computer Science, University of Melbourne, Australia,
1993.
Thorn, J.A. and Zobel, J.A. NU-Prolog reference manual, Version 1.3. Technical
Report, Department of Computer Science, University of Melbourne, Australia,
1988.
Ullman, J.D. Implementation of logical query languages for databases. A C M Trans-
actions on Database Systems, 10(3):289-321, 1985.
Vaghani, J., Ramamohanarao, K., Kemp, D.B., Somogyi, Z., and Stuckey, P.J.
Design overview of the Aditi deductive database system. Proceedings of the
Seventh International Conference on Data Engineering Kobe, Japan, 1991.
Vieille, L. Recursive query processing: Fundamental algorithms and the DedGin
system. In: Gray, P. and Lucas, R., eds., Prolog and Databases. Chicester,
England: Ellis Horwood, 1988.
Vieille, L., Bayer, P., Kfichenhoff, V., and Lefebvre, A. EKS-VI: A short overview.
Proceedings of the AAA1 Workshop on Knowledge Base Systems, Boston, MA, 1990.