Skip to content

Commit d390886

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 83a0ad2 commit d390886

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.1.1.1.2.1 1996/08/24 20:53:09 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.1.1.1.2.1 1996/08/24 20:52:17 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.1.1.1.2.1 1996/08/24 20:53:26 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.2 1996/07/23 02:23:15 scrappy Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.2.2.1 1996/08/24 20:53:39 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)
@@ -194,6 +201,9 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
194201

195202
length = tuple->t_len - tuple->t_hoff;
196203
fwrite(&length, sizeof(int32), 1, fp);
204+
if (oids)
205+
fwrite((char *) &tuple->t_oid, sizeof(int32), 1, fp);
206+
197207
fwrite(&null_ct, sizeof(int32), 1, fp);
198208
if (null_ct > 0) {
199209
for (i = 0; i < attr_count; i++) {
@@ -219,7 +229,7 @@ CopyTo(Relation rel, bool binary, FILE *fp, char *delim)
219229
}
220230

221231
static void
222-
CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
232+
CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
223233
{
224234
HeapTuple tuple;
225235
IndexTuple ituple;
@@ -257,7 +267,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
257267
int n_indices;
258268
InsertIndexResult indexRes;
259269
TupleDesc tupDesc;
260-
270+
Oid loaded_oid;
271+
261272
tupDesc = RelationGetTupleDescriptor(rel);
262273
attr = tupDesc->attrs;
263274
attr_count = tupDesc->natts;
@@ -371,8 +382,18 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
371382

372383
while (!done) {
373384
if (!binary) {
385+
if (oids) {
386+
string = CopyReadAttribute(fp, &isnull, delim);
387+
if (string == NULL)
388+
done = 1;
389+
else {
390+
loaded_oid = oidin(string);
391+
if (loaded_oid < BootstrapObjectIdData)
392+
elog(WARN, "COPY TEXT: Invalid Oid");
393+
}
394+
}
374395
for (i = 0; i < attr_count && !done; i++) {
375-
string = CopyReadAttribute(i, fp, &isnull, delim);
396+
string = CopyReadAttribute(fp, &isnull, delim);
376397
if (isnull) {
377398
values[i] = PointerGetDatum(NULL);
378399
nulls[i] = 'n';
@@ -398,6 +419,11 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
398419
if (feof(fp)) {
399420
done = 1;
400421
}else {
422+
if (oids) {
423+
fread(&loaded_oid, sizeof(int32), 1, fp);
424+
if (loaded_oid < BootstrapObjectIdData)
425+
elog(WARN, "COPY BINARY: Invalid Oid");
426+
}
401427
fread(&null_ct, sizeof(int32), 1, fp);
402428
if (null_ct > 0) {
403429
for (i = 0; i < null_ct; i++) {
@@ -473,6 +499,8 @@ CopyFrom(Relation rel, bool binary, FILE *fp, char *delim)
473499

474500
tupDesc = CreateTupleDesc(attr_count, attr);
475501
tuple = heap_formtuple(tupDesc, values, nulls);
502+
if (oids)
503+
tuple->t_oid = loaded_oid;
476504
heap_insert(rel, tuple);
477505

478506
if (has_index) {
@@ -696,7 +724,7 @@ inString(char c, char* s)
696724
*/
697725

698726
static char *
699-
CopyReadAttribute(int attno, FILE *fp, bool *isnull, char *delim)
727+
CopyReadAttribute(FILE *fp, bool *isnull, char *delim)
700728
{
701729
static char attribute[EXT_ATTLEN];
702730
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.1.1.1.2.1 1996/08/24 20:53:43 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.1.1.1 1996/07/09 06:21:33 scrappy Exp $
9+
* $Id: parsenodes.h,v 1.1.1.1.2.1 1996/08/24 20:53:55 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)