Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 208a30f

Browse files
committed
The patch does several things:
It adds a WITH OIDS option to the copy command, which allows dumping and loading of oids. If a copy command tried to load in an oid that is greater than its current system max oid, the system max oid is incremented. No checking is done to see if other backends are running and have cached oids. pg_dump as its first step when using the -o (oid) option, will copy in a dummy row to set the system max oid value so as rows are loaded in, they are certain to be lower than the system oid. pg_dump now creates indexes at the end to speed loading Submitted by: Bruce Momjian <maillist@candle.pha.pa.us>
1 parent 2adb6d7 commit 208a30f

File tree

13 files changed

+254
-83
lines changed

13 files changed

+254
-83
lines changed

src/backend/access/heap/heapam.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.1.1.1 1996/07/09 06:21:11 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.2 1996/08/24 20:47:54 scrappy Exp $
1111
*
1212
*
1313
* INTERFACE ROUTINES
@@ -1093,6 +1093,8 @@ heap_insert(Relation relation, HeapTuple tup)
10931093
tup->t_oid = newoid();
10941094
LastOidProcessed = tup->t_oid;
10951095
}
1096+
else
1097+
CheckMaxObjectId(tup->t_oid);
10961098

10971099
TransactionIdStore(GetCurrentTransactionId(), &(tup->t_xmin));
10981100
tup->t_cmin = GetCurrentCommandId();

src/backend/access/transam.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: transam.h,v 1.1.1.1 1996/07/09 06:21:09 scrappy Exp $
9+
* $Id: transam.h,v 1.2 1996/08/24 20:47:42 scrappy Exp $
1010
*
1111
* NOTES
1212
* Transaction System Version 101 now support proper oid
@@ -46,6 +46,14 @@
4646

4747
typedef unsigned char XidStatus; /* (2 bits) */
4848

49+
/* ----------
50+
* note: we reserve the first 16384 object ids for internal use.
51+
* oid's less than this appear in the .bki files. the choice of
52+
* 16384 is completely arbitrary.
53+
* ----------
54+
*/
55+
#define BootstrapObjectIdData 16384
56+
4957
/* ----------------
5058
* BitIndexOf computes the index of the Nth xid on a given block
5159
* ----------------
@@ -182,6 +190,7 @@ extern void GetNewTransactionId(TransactionId *xid);
182190
extern void UpdateLastCommittedXid(TransactionId xid);
183191
extern void GetNewObjectIdBlock(Oid *oid_return, int oid_block_size);
184192
extern void GetNewObjectId(Oid *oid_return);
193+
extern void CheckMaxObjectId(Oid assigned_oid);
185194

186195
/* ----------------
187196
* global variable extern declarations

src/backend/access/transam/varsup.c

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.1.1.1 1996/07/09 06:21:13 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/access/transam/varsup.c,v 1.2 1996/08/24 20:48:04 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,14 +28,6 @@
2828

2929
#include "catalog/catname.h"
3030

31-
/* ----------
32-
* note: we reserve the first 16384 object ids for internal use.
33-
* oid's less than this appear in the .bki files. the choice of
34-
* 16384 is completely arbitrary.
35-
* ----------
36-
*/
37-
#define BootstrapObjectIdData 16384
38-
3931
/* ---------------------
4032
* spin lock for oid generation
4133
* ---------------------
@@ -604,3 +596,62 @@ GetNewObjectId(Oid *oid_return) /* place to return the new object id */
604596
next_prefetched_oid++;
605597
prefetched_oid_count--;
606598
}
599+
600+
void
601+
CheckMaxObjectId(Oid assigned_oid)
602+
{
603+
Oid pass_oid;
604+
605+
606+
if (prefetched_oid_count == 0) /* make sure next/max is set, or reload */
607+
GetNewObjectId(&pass_oid);
608+
609+
/* ----------------
610+
* If we are below prefetched limits, do nothing
611+
* ----------------
612+
*/
613+
614+
if (assigned_oid < next_prefetched_oid)
615+
return;
616+
617+
/* ----------------
618+
* If we are here, we are coming from a 'copy from' with oid's
619+
*
620+
* If we are in the prefetched oid range, just bump it up
621+
*
622+
* ----------------
623+
*/
624+
625+
if (assigned_oid <= next_prefetched_oid + prefetched_oid_count - 1)
626+
{
627+
prefetched_oid_count -= assigned_oid - next_prefetched_oid + 1;
628+
next_prefetched_oid = assigned_oid + 1;
629+
return;
630+
}
631+
632+
/* ----------------
633+
* We have exceeded the prefetch oid range
634+
*
635+
* We should lock the database and kill all other backends
636+
* but we are loading oid's that we can not guarantee are unique
637+
* anyway, so we must rely on the user
638+
*
639+
*
640+
* We now:
641+
* set the variable relation with the new max oid
642+
* force the backend to reload its oid cache
643+
*
644+
* We use the oid cache so we don't have to update the variable
645+
* relation every time
646+
*
647+
* ----------------
648+
*/
649+
650+
pass_oid = assigned_oid;
651+
VariableRelationPutNextOid(&pass_oid); /* not modified */
652+
prefetched_oid_count = 0; /* force reload */
653+
pass_oid = assigned_oid;
654+
GetNewObjectId(&pass_oid); /* throw away returned oid */
655+
656+
}
657+

src/backend/commands/copy.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.3 1996/08/14 05:33:04 scrappy Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.4 1996/08/24 20:48:14 scrappy Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -22,11 +22,13 @@
2222
#include "catalog/pg_index.h"
2323
#include "catalog/index.h"
2424

25+
#include "storage/bufmgr.h"
2526
#include "access/heapam.h"
2627
#include "access/htup.h"
2728
#include "access/itup.h"
2829
#include "access/relscan.h"
2930
#include "access/funcindex.h"
31+
#include "access/transam.h"
3032
#include "access/tupdesc.h"
3133
#include "nodes/execnodes.h"
3234
#include "nodes/plannodes.h"
@@ -50,23 +52,23 @@
5052
static bool reading_from_input = false;
5153

5254
/* non-export function prototypes */
53-
static void CopyTo(Relation rel, bool binary, FILE *fp, char *delim);
54-
static void CopyFrom(Relation rel, bool binary, FILE *fp, char *delim);
55+
static void CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim);
56+
static void CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim);
5557
static Oid GetOutputFunction(Oid type);
5658
static Oid GetTypeElement(Oid type);
5759
static Oid GetInputFunction(Oid type);
5860
static Oid IsTypeByVal(Oid type);
5961
static void GetIndexRelations(Oid main_relation_oid,
6062
int *n_indices,
6163
Relation **index_rels);
62-
static char *CopyReadAttribute(int attno, FILE *fp, bool *isnull, char *delim);
64+
static char *CopyReadAttribute(FILE *fp, bool *isnull, char *delim);
6365
static void CopyAttributeOut(FILE *fp, char *string, char *delim);
6466
static int CountTuples(Relation relation);
6567

6668
extern FILE *Pfout, *Pfin;
6769

6870
void
69-
DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
71+
DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, char *filename,
7072
char *delim)
7173
{
7274
FILE *fp;
@@ -86,7 +88,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
8688
if (fp == NULL) {
8789
elog(WARN, "COPY: file %s could not be open for reading", filename);
8890
}
89-
CopyFrom(rel, binary, fp, delim);
91+
CopyFrom(rel, binary, oids, fp, delim);
9092
}else {
9193

9294
mode_t oumask = umask((mode_t) 0);
@@ -102,7 +104,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
102104
if (fp == NULL) {
103105
elog(WARN, "COPY: file %s could not be open for writing", filename);
104106
}
105-
CopyTo(rel, binary, fp, delim);
107+
CopyTo(rel, binary, oids, fp, delim);
106108
}
107109
if (!pipe) {
108110
fclose(fp);
@@ -113,7 +115,7 @@ DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
113115
}
114116

115117
static void
116-
CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
118+
CopyTo(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
117119
{
118120
HeapTuple tuple;
119121
HeapScanDesc scandesc;
@@ -159,6 +161,11 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
159161
for (tuple = heap_getnext(scandesc, 0, NULL);
160162
tuple != NULL;
161163
tuple = heap_getnext(scandesc, 0, NULL)) {
164+
165+
if (oids && !binary) {
166+
fputs(oidout(tuple->t_oid),fp);
167+
fputc(delim[0], fp);
168+
}
162169

163170
for (i = 0; i < attr_count; i++) {
164171
value = (Datum)
@@ -197,6 +204,9 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
197204

198205
length = tuple->t_len - tuple->t_hoff;
199206
fwrite(&length, sizeof(int32), 1, fp);
207+
if (oids)
208+
fwrite((char *) &tuple->t_oid, sizeof(int32), 1, fp);
209+
200210
fwrite(&null_ct, sizeof(int32), 1, fp);
201211
if (null_ct > 0) {
202212
for (i = 0; i < attr_count; i++) {
@@ -222,7 +232,7 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
222232
}
223233

224234
static void
225-
CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
235+
CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
226236
{
227237
HeapTuple tuple;
228238
IndexTuple ituple;
@@ -260,7 +270,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
260270
int n_indices;
261271
InsertIndexResult indexRes;
262272
TupleDesc tupDesc;
263-
273+
Oid loaded_oid;
274+
264275
tupDesc = RelationGetTupleDescriptor(rel);
265276
attr = tupDesc->attrs;
266277
attr_count = tupDesc->natts;
@@ -374,8 +385,18 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
374385

375386
while (!done) {
376387
if (!binary) {
388+
if (oids) {
389+
string = CopyReadAttribute(fp, &isnull, delim);
390+
if (string == NULL)
391+
done = 1;
392+
else {
393+
loaded_oid = oidin(string);
394+
if (loaded_oid < BootstrapObjectIdData)
395+
elog(WARN, "COPY TEXT: Invalid Oid");
396+
}
397+
}
377398
for (i = 0; i < attr_count && !done; i++) {
378-
string = CopyReadAttribute(i, fp, &isnull, delim);
399+
string = CopyReadAttribute(fp, &isnull, delim);
379400
if (isnull) {
380401
values[i] = PointerGetDatum(NULL);
381402
nulls[i] = 'n';
@@ -401,6 +422,11 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
401422
if (feof(fp)) {
402423
done = 1;
403424
}else {
425+
if (oids) {
426+
fread(&loaded_oid, sizeof(int32), 1, fp);
427+
if (loaded_oid < BootstrapObjectIdData)
428+
elog(WARN, "COPY BINARY: Invalid Oid");
429+
}
404430
fread(&null_ct, sizeof(int32), 1, fp);
405431
if (null_ct > 0) {
406432
for (i = 0; i < null_ct; i++) {
@@ -476,6 +502,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
476502

477503
tupDesc = CreateTupleDesc(attr_count, attr);
478504
tuple = heap_formtuple(tupDesc, values, nulls);
505+
if (oids)
506+
tuple->t_oid = loaded_oid;
479507
heap_insert(rel, tuple);
480508

481509
if (has_index) {
@@ -699,7 +727,7 @@ inString(char c, char* s)
699727
*/
700728

701729
static char *
702-
CopyReadAttribute(int attno, FILE *fp, bool *isnull, char *delim)
730+
CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
703731
{
704732
static char attribute[EXT_ATTLEN];
705733
char c;

src/backend/commands/copy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: copy.h,v 1.1.1.1 1996/07/09 06:21:19 scrappy Exp $
9+
* $Id: copy.h,v 1.2 1996/08/24 20:48:16 scrappy Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -15,7 +15,7 @@
1515

1616
#include "postgres.h"
1717

18-
void DoCopy(char *relname, bool binary, bool from, bool pipe, char *filename,
18+
void DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe, char *filename,
1919
char *delim);
2020

2121
#endif /* COPY_H */

src/backend/nodes/parsenodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: parsenodes.h,v 1.3 1996/08/06 16:37:53 scrappy Exp $
9+
* $Id: parsenodes.h,v 1.4 1996/08/24 20:48:31 scrappy Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -114,6 +114,7 @@ typedef struct CopyStmt {
114114
NodeTag type;
115115
bool binary; /* is a binary copy? */
116116
char *relname; /* the relation to copy */
117+
bool oids; /* copy oid's? */
117118
int direction; /* TO or FROM */
118119
char *filename; /* if NULL, use stdin/stdout */
119120
char *delimiter; /* delimiter character, \t by default*/

0 commit comments

Comments
 (0)